我是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>
它重定向到仪表板页面,但由于令牌
,我在该页面中使用的网络服务无法正常工作错误:
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)
尝试更改以下行,可能需要承诺success
$http.get("http://103.19.89.152:8080/ccp-services/dashboardearnfit/fetch", config).then(function(data) {
$scope.dashboardData = data;
});
问候。
答案 1 :(得分:0)
看起来你正在注射一个不可用的模块。在这种情况下,它看起来像'AuthService&#39;当您尝试作为依赖项注入时,它不可用。
您已声明了两个单独的应用程序(在这种情况下不会共享依赖项)。
1:这是一个模块&#39; setter&#39;使用数组语法。
var app = angular.module('loginFormApp', []);
2:这也是一个带有数组语法的模块设置器。
var app = angular.module('myApp', []);
将2更改为getter,这意味着应用程序将共享模块。
var app = angular.module('loginFormApp');
或重组您的模块/应用程序依赖项。
答案 2 :(得分:0)
假设您使用了这样的ng-app:
ng-app="myApp"
然后,您必须使用依赖项链接模块:
var app = angular.module('myApp', ['loginFormApp']);
或者模块'myApp'的注入器对loginFormApp
一无所知答案 3 :(得分: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;
}
}
});