如何适当地'$ inject`参数?

时间:2016-08-20 12:32:42

标签: javascript angularjs dependency-injection

当我没有向$inject方法插入任何参数时,它完全有效:

(function()
    {        
       var FooController=function($scope){           
                     alert('a1');
                     debugger;
        };

        FooController.$inject=['$scope'];//it perfectly works            
        angular.module('someApp',[]).controller('FooController', FooController);
    }());

但是,如果我向$inject方法添加一些参数,则它不起作用:

(function()
{   
    var FooController=function($scope, foo, bar) { 
                 alert('a1');
                 debugger;
    };

    var foo={type:"Fiat", model:"500", color:"white"};
    var bar={type:"Fiat", model:"500", color:"white"};

    FooController.$inject=['$scope', 'foo', 'bar'];//it doesn't work
    angular.module('someApp',[]).controller('FooController', FooController);
}());

我试图放alert('a1'),但它没有运行。所以在我看来,我不恰当地宣布了以下一行:

FooController.$inject=['$scope', 'foo', 'bar'];//it doesn't work
你可以澄清一下我做错了什么吗?

1 个答案:

答案 0 :(得分:2)

如何创建专用的value服务?

(function()
{   
    var FooController=function($scope, foo, bar) { 
                 alert('a1');
                 debugger;
    };

    var foo={type:"Fiat", model:"500", color:"white"};
    var bar={type:"Fiat", model:"500", color:"white"};

    FooController.$inject=['$scope', 'foo', 'bar'];
    angular.module('someApp',[])
      .controller('FooController', FooController)
      .value('foo', foo)
      .value('bar', bar)
}());