2016-06-10 41 views
1

另一個js函數public方法我創建了定製服務調用從內部供水

(function() { 
    "use strict"; 

    angular 
     .module("common.services")   
     .factory("redirectService", 
       ["$q", "$location", 
       redirectService]) 
     .config(function ($httpProvider) { 
      $httpProvider.interceptors.push('redirectService'); 
     }); 

    function redirectService($q, $location){ 
     ... 
     var redirect = function() { 
      ... 
     }; 

     return {   
      doRedirect: redirect 
     }; 
    } 

我在那裏我注入這redirectService我試圖調用此發佈doRedirect方法等控制器內部

angular 
    .module("myModule") 
    .controller("MyController", 
       ["$scope", 
       "redirectService"     
        MyController]); 

    function MyController(redirectService){ 
     vm.doClick = function() { 
     redirectService.doRedirect(); 
     } 
    } 

這裏我得到錯誤調用doRedirect方法

Error: redirectService.doRedirect is not a function

+1

難道是loginRedirectService應該重定向服務電話 – Thargor

+0

您創建重定向功能,但嘗試返回redirectPostLogin。或者你錯誤地粘貼了代碼? – speedingdeer

回答

2

,您已經對依賴數組和函數參數參數的個數爲MyController

變化的不平衡

function MyController(redirectService){ 

function MyController($scope, redirectService){ 
0

myController的功能有兩個參數第一$範圍,第二redirectService

angular 
    .module("myModule") 
    .controller("MyController", 
       ["$scope", 
       "redirectService"     
        MyController]); 

    function MyController($scope, redirectService){ 
     vm.doClick = function() { 
     redirectService.doRedirect(); 
     } 
    }