我是角度js的新手,现在我正在尝试使用工厂来使代码更清晰。但现在我收到以下错误:
$ injector:unpr(未知提供商)。
我卡在这里,无法找到问题所在。服务和这个控制器实际上是在不同的文件中,然后我试图把它们放在一起,但它仍然无法正常工作。如果你能帮助我找出问题,我将非常高兴。
var app = angular.module('myApp', ['ngRoute']);
app.factory('AuthenticationService', ['$scope', '$location', function($scope, $location){
var currentUser;
return {
login: function(username, password){
var endPoint = "http://localhost:8080/RestServer/main/just/Authorize";
var jsonData = {
username: username,
password: password
};
var jsonString = JSON.stringify(jsonData);
$http.post(endPoint, jsonString,{headers: {'Content-Type': 'application/json'}}).success(function (data,status, headers ){
if(data.access == "ok")
{
$location.path("learning");
}
else
{
$location.path("error");
}
});
},
logout: function(){},
isLoggedIn: function(){},
curUser: function(){return currentUser}
};
}
]);
app.controller('loginController',['$scope', 'AuthenticationService', function($scope, AuthenticationService)
{
$scope.login = function()
{
AuthenticationService.login($scope.username, $scope.password);
}
}]);
这是html:
<html data-ng-app="myApp">
<head>
<script src="https://code.angularjs.org/1.5.0-rc.0/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-rc.0/angular-route.min.js"></script>
<script src="Scripts/app.js"></script>
<script src="Scripts/services.js"></script>
<script src="Scripts/controllers.js"></script>
<link href="CSS/bootstrap.min.css" rel="stylesheet">
<title>Welcome!</title>
</head>
<body>
<div class="container theme-showcase" role="main" ng-view="">
</div>
</body>
答案 0 :(得分:1)
只需从工厂中删除$scope
,您通常不会在工厂,服务或提供商中使用$ scope,也可以将$http
添加为依赖项,因为您正在进行$ http调用
app.factory('AuthenticationService', [ '$location','$http', function( $location,$http){
}
这是工作Application