簡體   English   中英

angular.js:12520錯誤:[$ injector:unpr]

[英]angular.js:12520Error: [$injector:unpr]

我是angularjs的新手,我正在使用基於令牌的身份驗證過程的應用程序,我創建了一個身份驗證服務,通過該服務我可以獲取令牌,但是當我在其他控制器中使用此服務時,我會得到$ injector: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

嘗試更改以下幾行可能需要先承諾再success

$http.get("http://103.19.89.152:8080/ccp-services/dashboardearnfit/fetch", config).then(function(data) {
    $scope.dashboardData = data;
});

問候。

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

您已經聲明了兩個單獨的應用程序(在這種情況下將不會共享依賴項)。

1:這是具有數組語法的模塊“設置程序”。

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

2:這也是具有數組語法的模塊設置器。

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

更改2:使用吸氣劑,這意味着應用程序將共享模塊。

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

或重組您的模塊/應用程序依賴項。

假設您像這樣使用ng-app:

ng-app="myApp"

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

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

否則模塊“ myApp”的注入程序將不了解loginFormApp

您可以按以下方式注入“ 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;

         }

     }
 });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM