0

我是新來angularjs,我的工作,它使用基於令牌的認證過程的應用程序,我創建了一個身份驗證服務,通過它,我得到令牌但是當我使用此服務在其他控制器我越來越$注射器:unpr錯誤任何幫助將明顯。

我的服務:authService.js

var app = angular.module('loginFormApp', []); 
app.controller('loginFormCtrl', function ($scope, AuthService) { 
    $scope.loginCall = function() { 
     AuthService.authentication($scope.loginId, $scope.password).then(function (token) { 
      //AuthService.getToken();    
     }); 


    }; 
}); 

app.factory('AuthService', function ($http) { 
    var cachedToken; 
    return { 
     authentication: function (UserName, Password) { 
      return $http.post("http://103.19.89.152:8080/ccp-services/authenticate", { 
        'userName': UserName, 
        'password': Password 
       }) 
       .then(function (response) { 
         window.location.href = "http://192.168.1.148:2000/angular/dashboard.html"; 
         cachedToken = response.data.httpHeaders.h5cAuthToken; 
         return cachedToken; 
         // alert(token); 

        }, 
        // Error Handling 
        function (response) { 
         console.log(response.datas); 
        }); 

     }, 
     getToken: function() { 
      //alert(cachedToken); 
      return cachedToken; 

     } 

    } 
}); 

我的儀表盤控制器:dashboard.html

<script> 
     var app = angular.module('myApp', ['loginFormApp']); 
     app.controller('dashboardFormCtrl', function($scope, $http, AuthService) { 
      var config = { 
       headers: { 
        'h5cAuthToken': AuthService.getToken(), 
        'Accept': 'application/json;odata=verbose' 
       } 
      }; 
      //alert(config.headers.h5cAuthToken); 

      $http.get("http://103.19.89.152:8080/ccp-services/dashboardearnfit/fetch", config).success(function(data) { 

       $scope.dashboardData = data; 
           }); 


     }); 

    </script> 

它重定向到儀表板頁面,但我正在使用web服務的網頁不是由於工作代幣

錯誤:

以下行
Navigated to http://192.168.1.148:2000/angular/dashboard.html 
dashboard.html:126 GET http://192.168.1.148:2000/angular/%7B%7BdashboardData.singleResult.userProfile.memberPhotoPath%7D%7D 404 (Not Found) 
dashboard.html:216 GET http://192.168.1.148:2000/angular/%7B%7Bactivityitem.participantPicPath%7D%7D 404 (Not Found) 
dashboard.html:289 GET http://192.168.1.148:2000/angular/%7B%7BchallengeItem.participantPicPath%7D%7D 404 (Not Found) 
jquery-migrate-1.1.0.min.js:1'//@ sourceURL' and '//@ sourceMappingURL' are deprecated, please use '//# sourceURL=' and '//# sourceMappingURL=' instead. 
angular.js:10765 GET http://103.19.89.152:8080/ccp-services/dashboardearnfit/fetch 403 (Forbidden)(anonymous function) @ angular.js:10765r @ angular.js:10558g @ angular.js:10268(anonymous function) @ angular.js:14792$eval @ angular.js:16052$digest @ angular.js:15870$apply @ angular.js:16160(anonymous function) @ angular.js:1679e @ angular.js:4523c @ angular.js:1677yc @ angular.js:1697Zd @ angular.js:1591(anonymous function) @ angular.js:29013b @ angular.js:3057If @ angular.js:3346d @ angular.js:3334 
+0

你能發佈完整的錯誤代碼嗎? –

+0

@Itsik,我更新了錯誤信息 –

+0

嘗試在沒有

0

看起來你正在注入一個不可用的模塊。在這種情況下,當您嘗試注入爲依賴項時,它看起來像'AuthService'不可用。

您已經定義了兩個獨立的應用程序(依賴不會在這種情況下共享)。

1:這是一個帶數組語法的模塊'setter'。

var app = angular.module('loginFormApp', []); 

2:這也是一個帶數組語法的模塊設置器。

var app = angular.module('myApp', []); 

更改2:對於getter,這意味着應用程序將共享模塊。

var app = angular.module('loginFormApp'); 

或重構您的模塊/應用程序依賴關係。

+0

我有兩個不同的頁面的login.html和dashboard.html所以我應該有兩個像VAR應用= angular.module(「對myApp」頁面使用相同的模塊名稱,[]); –

+0

我authService.js和login.html的編輯並在這兩個文件被稱爲對myApp犯同樣的模塊名稱,請查看更新的authService.js –

+0

第一:404回覆:http://192.168.1.148:2000/angular/%7B %7BdashboardData.singleResult.userProfile.memberPhotoPath%7D%7D。 %7B代表'{'。我的猜測是你有這樣的圖像:''使用'ng-src'代替:''。第二個403意味着禁止,請檢查您的服務器的日誌。 – Walfrat

0

假如你使用的NG-應用程序是這樣的:

ng-app="myApp" 

然後,您必須使用依賴鏈接模塊:

var app = angular.module('myApp', ['loginFormApp']); 

或模塊 '對myApp' 的注射器將一無所知loginFormApp

+0

我鏈接使用依賴模塊,因爲它消除了$注射器:unpr錯誤,但錯誤403來了,正如我在代碼 –

+0

更新,因爲它導航到dashboard.html頁面,但爲gettoken()函數返回未定義的對象 –

0

您可以注入'AuthService',如下所示:

var app = angular.module('loginFormApp', []); 
app.controller('loginFormCtrl',['$scope', 'AuthService', function ($scope, AuthService) { 
    $scope.loginCall = function() { 
     AuthService.authentication($scope.loginId, $scope.password).then(function (token) { 
      //AuthService.getToken();    
     }); 


    }; 
}]); 

app.factory('AuthService', function ($http) { 
    var cachedToken; 
    return { 
     authentication: function (UserName, Password) { 
      return $http.post("http://103.19.89.152:8080/ccp-services/authenticate", { 
        'userName': UserName, 
        'password': Password 
       }) 
       .then(function (response) { 
         window.location.href = "http://192.168.1.148:2000/angular/dashboard.html"; 
         cachedToken = response.data.httpHeaders.h5cAuthToken; 
         return cachedToken; 
         // alert(token); 

        }, 
        // Error Handling 
        function (response) { 
         console.log(response.datas); 
        }); 

     }, 
     getToken: function() { 
      //alert(cachedToken); 
      return cachedToken; 

     } 

    } 
}); 
+0

@ashish庫馬爾 - PLZ檢查上面的代碼。 –

相關問題