StepUp StepUp - 3 months ago 12
Javascript Question

How to appropriately `$inject ` with parameters?

When I do not insert any parameters to

$inject
method, then it perfectly works:

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

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


However, if I add some parameters to
$inject
method, then it does not work:

(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);
}());


I've tried to put
alert('a1')
, but it is not run. So in my view, I declared the following row inappropriately:

FooController.$inject=['$scope', 'foo', 'bar'];//it doesn't work


Could you clarify what I am doing wrong?

Answer

What about creating dedicated value services?

(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)
}());