angularjs - $provide outside config blocks -


i'm missing fundamental point injector, fail understand why this

angular.module('app').config(function ($provide) {     ... }); 

and this

angular.module('app').config(function ($injector) {     $injector.invoke(function ($provide) { ... }); }); 

work intended, while this

app.run(function($provide) {     ... }); 

will throw

error: [$injector:unpr] unknown provider: $provideprovider <- $provide

as follows above, config has special relationship providers, while run deals instances, yet i'm unsure thing makes config blocks special.

as consequence of that, there no way $provide outside config blocks, e.g. angular.injector() (though seems gets provider instances also)?

the question, besides mere curiosity, has practical considerations. in 1.4 of $provide functions exposed module, that's not true 1.3.

the purpose of config() function allow perform global configuration affect entire application - includes services, directives, controllers, etc. because of that, config() block must run before else. but, still need way perform aforementioned configuration , make available rest of app. , way using providers.

what makes providers "special" have 2 initialization parts, , 1 of them directly related config() block. take @ following code:

app.provider('myservice', function() {     var self = {};         this.setsomeglobalproperty = function(value) {         self.someglobalproperty = value;     };      this.$get = function(somedependency) {         this.dosomething = function() {             console.log(self.someglobalproperty);         };     };     });  app.config(function(myserviceprovider) {     myserviceprovider.setsomeglobalproperty('foobar'); });  app.controller('myctrl', function(myservice) {     myservice.dosomething(); }); 

when inject provider config() function, can access but $get function (technically can access $get function, calling won't work). way can whatever configuration might need do. that's first initialization part. it's worth mentioning though our service called myservice, need use suffix provider here.

but when inject same provider other place, angular calls $get() function , injects whatever returns. that's second initialization part. in case, provider behaves ordinary service.

now $provide , $injector. since "configuration services", makes sense me can't access them outside config() block. if could, able to, say, create factory after had been used service.

finally, haven't played v1.4 yet, have no idea why behavior apparently has changed. if knows why, please let me know , i'll update answer.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -