2016-10-07 11 views
0

私の.runブロックにサービスを注入したいのですが、注入すると使えません。角度のjsで.runでサービスを挿入できません

私のコードは次のとおりです。

.run(['AuthFactory', function ($state, $rootScope, AuthFactory, $location) { 

    console.log("Auth Factory :%O", AuthFactory); 

    AuthFactory.registerUserChangeHandler(function (currentUser) { 
     $rootScope.currentUser = currentUser; 
    }); 

    AuthFactory.refresh().then(function (currentUser) { 
     console.log("Current User is", currentUser); 
    }, function (reason) { 
     // User is not Logged in 
     $location.path("login"); 

    }); 
}]); 

私はこのコードを書くとき、私はエラーを取得する:私は単に機能でAuthFactoryを注入した場合

"app.js:120Uncaught TypeError: Cannot read property 'registerUserChangeHandler' of undefined"

は、すべてが正常に動作しますが、今はUserServiceを注入したいです&サービス内のメソッドを使用します。

私は機能に注入しようとしましたが、使用できません。

すべての提案を開きます。

答えて

0

誤っているのは、配列の依存関係注入を使用すると、シーケンスに従う必要があるからです。たとえば、最初の値がarrayの場合は$ state、コントローラのコールバックの場合は最初の値が$ stateを表します。

.run(['$state','$rootScope','AuthFactory','$location', function ($state, $rootScope, AuthFactory, $location) { 
      console.log("Auth Factory :%O", AuthFactory); 
//    UserService.login({ 
//     'username': "guest", 
//     'password': "guest" 
//    }, function() { 
//     $state.go("corporate_site.home", {reload:'true'}); 
//    }, function() { 
//     $rootScope.error = "Login Failed. Invalid Credentials."; 
//    }); 
//    $state.go("corporate_site.home", {reload: 'true'}); 

      AuthFactory.registerUserChangeHandler(function (currentUser) { 
      $rootScope.currentUser = currentUser; 
     }); 

     AuthFactory.refresh().then(function (currentUser) { 
      console.log("Current User is", currentUser); 
      }, function (reason) { 

//    User is not Logged in 
       $location.path("login"); 

      }); 
     }]); 
2

あなたはDIで混乱します。

.run(['state', '$rootScope', 'AuthFactory', '$location', 
    function ($state, $rootScope, AuthFactory, $location) { 

     console.log("Auth Factory :%O", AuthFactory); 
     // ... 
    }]); 
関連する問題