angular.module('MyController',
function($scope) {
$scope.shouldShowLogin = true;
$scope.showLogin = function() {
$scope.shouldShowLogin = !$scope.shouldShowLogin;
}
$scope.clickButton = function() {
$("#btn span").html("Clicked");
}
$scope.onLogin = function(user) {
$http({
method: 'POST',
url: '/login',
data: {
user: user
}
}).success(function(data) {
// user
})
}
})angular.module('MyController',
function($scope, UserSrv) {
// The content can be controlled by
// directives
$scope.onLogin = function(user) {
UserSrv.runLogin(user);
}
})module.controller('FooController', function($http){
//...
});
module.controller('BarController', function($window){
//...
});new FunctionYouPassedToService()
module.service('MyService', function() {
this.method1 = function() {
//..
}
this.method2 = function() {
//..
}
});module.factory('MyService', function() {
var factory = {};
factory.method1 = function() {
//..
}
factory.method2 = function() {
//..
}
return factory;
});angular.module('myApp')
.service('myService', ['$http', '$q', function($http, $q){
var deferObject,
this.getPromise: function() {
var promise = $http.get('/someURL'),
deferObject = deferObject || $q.defer();
promise.then(
// OnSuccess function
function(answer){
// This code will only run if we have a successful promise.
deferObject.resolve(answer);
},
// OnFailure function
function(reason){
// This code will only run if we have a failed promise.
deferObject.reject(reason);
});
return deferObject.promise;
}
};
}]);The major difference between the value() method and the constant() method is that you can inject a constant into a config function, whereas you cannot inject a value
angular.module('App').constant('key', value);
angular.module('App').value('key', value);angular.module('App', []).
config(function($provide) {
$provide.constant('key', value);
};Typically, a good rule of thumb is that we should use value() to register a service object or function, while we should use constant() for configuration data.
Future Link:
http://lostechies.com/gabrielschenker/2014/01/14/angularjspart-9-values-and-constants/