Angular Service
instance variable & public methods
by hamzeen hameem
HTML
<script src="http://code.angularjs.org/angular-1.0.1.js"></script>
<div ng-controller="MyCtrl">
Hello, {{name}}!<br/>
Your Age {{age}}
</div>
JavaScript
var myApp = angular.module('myApp',[]);
myApp.factory('UserService', function() {
return {
trigger: trigger, // public method
age: 20 // acts like an instance var
}; // publicly exposed from this service
function trigger(name,age) {
this.age = age;
return name;
}
});
function MyCtrl($scope, UserService) {
$scope.name = UserService.trigger("Angular Service", 48);
$scope.age = UserService.age;
}