AngularJS config block

Hello friends !

In this article, we will learn What is AngularJS config block ? How to configure AngularJS Application using Configuration block ?

Config block runs when the AngularJS module is loaded. The config block is used to configure the application.

As we know to register injectables directly with the module API as follows.

var ng = angular.module("myApp", []); ng.value('a', 123); ng.factory('a', function () { return 123; }); ng.directive('directiveName', function () { return { restrict: 'EA', scope: false, link: function (s, e, a, m) { }, }; }); ng.filter('filterName', function () { return function (i) { return Math.ceil(i); }; });

it is also possible to register services, directives etc. by injecting $provide or the individual service providers into the config function.

var ng = angular.module("myApp", []); ng.config(function ($provide, $compileProvider, $filterProvider) { $provide.value('a', 123); $provide.factory('a', function () { return 123; }); $compileProvider.directive('directiveName', function () { return { restrict: 'EA', scope: false, link: function (s, e, a, m) { }, }; }); $filterProvider.register('filterName', function () { return function (i) { return Math.ceil(i); }; }); });

Moreover, we can also initialize provider.

Lets create provider with name $service1 as follows.

myApp.provider('$service1', function () { var service = 'service1'; this.registerService = function (value) { service = value; }; this.$get = function() { var that = {}; that.Service2Method = function () { console.log(service); }; return that; }; });

Initializing provider in config function.

myApp.config(['$service1Provider', function ($service1Provider) { $service2Provider.registerService('changed service1'); }]);

Config block is executed only once in the lifetime of an Angular application.

If you have any query or question or topic on which, we might have to write an article for your interest or any kind of suggestion regarding this post, Just feel free to write us, by hit add comment button below or contact via Contact Us form.


Your feedback and suggestions will be highly appreciated. Also try to leave comments from your valid verified email account, so that we can respond you quickly.

 
 

{{c.Content}}

Comment By: {{c.Author}}  On:   {{c.CreatedDate|date:'dd/MM/yyyy'}} / Reply


Categories