Sunday, September 3, 2017

Service in AngularJS

  • It provides a method to keep data across the lifetime of the Angular app.
  • It provides a method to communicate data across the controllers in a consistent way.
  • This is a singleton object and it is instantiated only once per application.
  • It is used to organize and share data and functions across the application.
  • A service is a constructor function.
  • You don’t need to return a value.
Syntex: module. service(' ServiceName', function );

Example:


When to use
  •  Use it when you just want access to the common data and functionality. In other cases, stick to a factory.
  •  Use it when you want to create a simple service. For anything complex, opt for Factories. Because you don’t want to expose this complexity. This is called abstraction.


No comments:

Post a Comment