Talk about the configuration block and running block of angular

Time:2020-12-5

The configuration block configures the module or service during the registration and configuration of the provider. This is the only place that can be modified before the app starts. In addition, only providers and constants can be injected into the config

 angular.module('myapp',[])
 
.config(function($provider){
        //**
})

Is this code familiar? But do you know what the following code has to do with config?

  angular.module('myapp',[])

  .factory('myservice',function(){

        var service ={};

        return service;

  })

  .directive('mydirective',function(){

        return {

            template:'<a>baidu.com</a>'

            }

    })

In fact, angular will compile the above code into the following form first

angular.module('myapp',[])

.config(function($provider,$complileProvider))

 $provider.factory('myservice',function(){

     var service ={};

     return service;

 })

 $compileProvider.directive('mydirective',function(){

     return {

     template:'<a>baidu.com</a>'

     }

 })

It can be seen that everything is provided by the provider.

Let’s talk about the running block

angular.module('myapp',[])

.run(function($rootScope){

})

Run is equivalent to the traditional main method, with a parameter (initializefn) executed after the injector is created by angular.