I don't understand the use of $inject in controllers

后端 未结 4 1653
执笔经年
执笔经年 2020-11-28 23:50

I am totally confused about inject in Angular. I do not know where to use it and why. Is it only used with factory as described here?

myController.$inject =          


        
4条回答
  •  时光取名叫无心
    2020-11-29 00:22

    The way you should use $inject is:

    function ApplicationController($scope){
        $scope.greet = "Foo is Not Great!5";
    }
    
    ApplicationController.$inject = ['$scope','$ionic'];
    
    app.controller('ApplicationController', ApplicationController);
    

    We need to this as to protect the code from uglifying or minimization.

    function(firstName,lastName) may get turned into function(n,m).

    So for AngularJS it will break the code because $scope can be replaced by 's'. This is because without the $ sign the angularJS won't be able to recognize the code.

提交回复
热议问题