繁体   English   中英

AngularJS Factory在控制器中未定义

[英]Angularjs Factory is undefined in controller

我是angular js的新手,现在我正尝试使用工厂使代码更清晰。 但是现在我收到以下错误:

$ injector:unpr(未知提供者)。

我卡在这里,找不到问题所在。 Service和此控制器实际上位于不同的文件中,然后我尝试将它们放在一起,但仍然无法正常工作。 如果您能帮助我解决我的问题,我将非常高兴。

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>

只需从工厂中删除$scope ,通常就不在工厂,服务或提供者内部使用$ scope,也不要将$http作为依赖项添加,因为您正在进行$ http调用

app.factory('AuthenticationService', [ '$location','$http', function( $location,$http){
}

这是工作中的Application

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM