简体   繁体   中英

How to inject factory dependency into controller in angular?

Scenario:- I have a factory say testFactory. Now till now I was defining my controller as following:-

app.controller('testCtrl',function($scope,testFactory)
{
  testFactory.Method1(){ //working fine}
}

But now before minimizing the file I defined the controller as:-

app.controller('testCtrl',['$scope','testFactory', function(a,testFactory)
{
   testFactory.Method1() {//throws undefined error}
}

I tried this:-

 app.controller('testCtrl',['$scope','$rootScope','testFactory', function(a,$rootScope,testFactory)
{
   testFactory.Method1() {//still thows error- unable to resolve dependency}
}

Now how should I include my factory in such case?

I think you have some problem with the factory declaration, try this

var TestCtrl= function($scope,TestFactory) {
  // ...
}
TestCtrl.$inject = ['$scope', 'TestFactory'];
app.controller('TestCtrl', TestCtrl);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM