【问题标题】:Error: error:unpr Unknown Provider错误:错误:unpr 未知提供者
【发布时间】:2014-09-28 08:42:19
【问题描述】:

我的主控制器

var MyApp = angular.module('ionicApp', ['ionic', 'MobiNav', 'authFactory']);

控制器消费工厂

MyApp.controller('AuthUser', ['$scope', 'authFactoryService', function ($scope, authFactoryService) {
    $scope.showForm = true;
    $scope.UserDataLogin = function () {
        var loginData = {};
        $scope.registration = {
            userName: $scope.Auth.userName,
            password: $scope.Auth.password,
            confirmPassword: $scope.Auth.password
        };
        authFactoryService.SaveRegistration(registration);
        window.scope = loginData;
    };
}
]
);

这是单独文件中的工厂

   var AuthService = angular.module('authFactory', []);
AuthService.factory('authFactoryService', [
    '$http', '$q', '$scope', function ($http, $q, $scope) {
    return {
        SaveRegistration: function() {
            var urlBase = 'http://localhost:48868/';
            $http.post(urlBase + 'api/account/register', registration).then(function(response) {
                    $scope.savedSuccessfully = true;
                    $scope.message = "User has been registered successfully, you will be redicted to login page in 2 seconds.";
                },
                function(response) {
                    var errors = [];
                    for (var key in response.data.modelState) {
                        for (var i = 0; i < response.data.modelState[key].length; i++) {
                            errors.push(response.data.modelState[key][i]);
                        }
                    }
                    $scope.message = "Failed to register user due to:" + errors.join(' ');
                });
        }

    };
}]);

错误我得到了什么 错误:[$injector:unpr]http://errors.angularjs.org/1.2.17/$injector/unpr?p0=copeProvider%20%3C-%20%24scope%20%3C-%20authFactoryService 在错误(本机)

为什么无法加载authFactoryService服务

【问题讨论】:

    标签: angularjs angularjs-service


    【解决方案1】:

    终于想通了, $scope 再次注入 Factory 更换 这个

    AuthService.factory('authFactoryService', [
        '$http', '$q', '$scope', function ($http, $q, $scope) {}]);
    

    对此(只是删除了再次注入工厂以进行依赖的$scope。

       AuthService.factory('authFactoryService', [
            '$http', '$q', function ($http, $q, $scope) {}]);
    

    【讨论】:

      【解决方案2】:

      var AuthService = angular.module('authFactory', []);

      您在模块中包含了一个空数组。这使它成为 模块设置器,覆盖现有模块。 要获取您使用的模块 angular.module('authFactory')

      问候 砂光机

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-12
      • 2013-02-21
      • 2013-01-25
      • 2015-01-25
      相关资源
      最近更新 更多