MOCKSTACKS
EN
Questions And Answers

More Tutorials









Angular Services


First define the service (in this case it uses the factory pattern):

.factory('dataService', function() {
 var dataObject = {};
 var service = {
 // define the getter method
 get data() {
 return dataObject;
 },
 // define the setter method
 set data(value) {
 dataObject = value || {};
 }
 };
 // return the "service" object to expose the getter/setter
 return service;
})
Now you can use the service to share data between controllers:
.controller('controllerOne', function(dataService) {
 // create a local reference to the dataService
 this.dataService = dataService;
 // create an object to store
 var someObject = {
 name: 'SomeObject',
 value: 1
 };
 // store the object
 this.dataService.data = someObject;
})
.controller('controllerTwo', function(dataService) {
 // create a local reference to the dataService
 this.dataService = dataService;
 // this will automatically update with any changes to the shared data object
 this.objectFromControllerOne = this.dataService.data;
})


create a Service


angular.module("app")
 .service("counterService", function(){
 var service = {
 number: 0
 };
 
 return service;
 });

use a service


angular.module("app")

// Custom services are injected just like Angular's built-in services
 .controller("step1Controller", ['counterService', '$scope', function(counterService,
$scope) {
 counterService.number++;
 // bind to object (by reference), not to value, for automatic sync
 $scope.counter = counterService;
 })


In the template using this controller you'd then write:

// editable
<input ng-model="counter.number" />


or

// read-only
<span ng-bind="counter.number"></span>


Of course, in real code you would interact with the service using methods on the controller, which in turn delegate to the service. The example above simply increments the counter value each time the controller is used in a template.

Services in Angularjs are singletons:

Services are singleton objects that are instantiated only once per app (by the $injector) and lazy loaded (created only when necessary).

Conclusion

In this page (written and validated by ) you learned about AngularJS Services . What's Next? If you are interested in completing AngularJS tutorial, your next topic will be learning about: AngularJS Service vs Factory.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.