Last active
August 29, 2015 14:13
-
-
Save drissamri/e6e234066e52455d6863 to your computer and use it in GitHub Desktop.
Linkshortener initialize AngularJS app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(angular) { | |
var linkApp = angular.module('linkApp', []); | |
}(angular)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (angular) { | |
'use strict'; | |
angular | |
.module('linkApp') | |
.controller('LinkController', ['$scope', '$log', 'LinkService', function LinkController($scope, $log, LinkService) { | |
$scope.longUrl = null; | |
$scope.link = null; | |
$scope.shorten = function (url) { | |
if (url) { | |
$log.log('Try to shorten: ' + url); | |
var promise = LinkService.shorten(url); | |
promise.then(function (link) { | |
$scope.link = link; | |
}); | |
} | |
}; | |
}]); | |
})(angular); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (angular) { | |
'use strict'; | |
angular | |
.module('linkApp') | |
.service('LinkService', ['$http', '$q', '$log', function LinkService($http, $q, $log) { | |
return { | |
shorten: function (url) { | |
var deferred = $q.defer(); | |
$http.get('/links?longUrl='+url) | |
.success(function(data) { | |
return deferred.resolve(data); | |
}).error(function(data) { | |
return deferred.resolve(data); | |
}); | |
return deferred.promise; | |
} | |
}; | |
}]); | |
})(angular); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment