MOCKSTACKS
EN
Questions And Answers

More Tutorials









Angular Providers

Provider


Provider is available both in configuration and run phases.

The Provider recipe is syntactically defined as a custom type that implements a $get method.

You should use the Provider recipe only when you want to expose an API for application-wide configuration that must be made before the application starts. This is usually interesting only for reusable services whose behavior might need to vary slightly between applications.

angular.module('app',[])
 .provider('endpointProvider', function() {
 var uri = 'n/a';
 
 this.set = function(value) {
 uri = value;
 };
 this.$get = function() {
 return {
 get: function() {
 return uri;
 }
 };
 };
 })
 .config(function(endpointProviderProvider) {
 endpointProviderProvider.set('http://some.rest.endpoint');
 }) 
 .controller('MainCtrl', function(endpointProvider) {
 var vm = this;
 vm.endpoint = endpointProvider.get();
 });
<body ng-controller="MainCtrl as vm">
 <div>endpoint = {{::vm.endpoint }}</div>
</body>

Without config phase result would be
endpoint = n/a

Factory


Factory is available in run phase.
The Factory recipe constructs a new service using a function with zero or more arguments (these are dependencies on other services). The return value of this function is the service instance created by this recipe.

Factory can create a service of any type, whether it be a primitive, object literal, function, or even an instance of a custom type.

angular.module('app',[])
 .factory('endpointFactory', function() {
 return {
 get: function() {
 return 'http://some.rest.endpoint';
 }
 };
 })
 .controller('MainCtrl', function(endpointFactory) {
 var vm = this;
 vm.endpoint = endpointFactory.get();
 });
<body ng-controller="MainCtrl as vm">
 <div>endpoint = {{::vm.endpoint }}</div>
</body>

Constant


Constant is available both in configuration and run phases.

angular.module('app',[])
 .constant('endpoint', 'http://some.rest.endpoint') // define
 .config(function(endpoint) {
 // do something with endpoint
 // available in both config- and run phases
 })
 .controller('MainCtrl', function(endpoint) { // inject
 var vm = this;
 vm.endpoint = endpoint; // usage
 });
<body ng-controller="MainCtrl as vm">
 <div>endpoint = {{ ::vm.endpoint }}</div>
</body>


Conclusion

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



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.