Angularjs未知提供程序错误

时间:2014-06-22 05:55:21

标签: javascript angularjs

我正在接受

Error: [$injector:unpr] Unknown provider: mvProfileCtrlProvider <- mvProfileCtrl

在我的错误中,我已经确定它包含在我的脚本中并且可以访问(我可以从chrome-debugger > sources > mvProfileCtrl.js打开它)因此我可以参考它。但这里还有两个相关的文件:

 angular.module('app').controller('navBarLoginController',function($scope,$http,mvIdentity,mvNotifier,mvAuth,mvProfileCtrl,$location) {
    $scope.identity = mvIdentity;
    $scope.signin = function(username,password) {
        mvAuth.authenticateUser(username,password).then(function(success){
            if(success){
                mvNotifier.notify('You have successfully signed in!');
            }else{
                mvNotifier.notify('Username/Password combination incorrect');
            }
        })
    }
    var profile = mvProfileCtrl;
    $scope.update = function(){
        profile.listen(mvIdentity.currentUser);
    }
    $scope.signout = function() {
        mvAuth.logoutUser().then(function() {
            $scope.username = "";
            $scope.password = "";
            mvNotifier.notify('You have successfully signed out!');
            $location.path('/');
        })
    }
})

mvProfileCtrl.js -

angular.module('app').controller('mvProfileCtrl', function($scope, mvAuth, mvIdentity, mvNotifier) {
    $scope.mvIdentity = mvIdentity;
    $scope.update = function() {
        console.log("" + $scope.updateemail + " -- " + test);
        var newUserData = {
            username: $scope.updateemail,
            firstName: $scope.updatefname,
            lastName: $scope.updatelname
        }
        if($scope.updatepassword && $scope.updatepassword.length > 0) {
            newUserData.password = $scope.updatepassword;
        }

        mvAuth.updateCurrentUser(newUserData).then(function() {
            $('#profileModal').modal('toggle');
            mvNotifier.notify('Your user account has been updated');
        }, function(reason) {
            mvNotifier.error(reason);
        })
    }
    return {
        listen : function(currentUser) {
            if (!currentUser) return;
            $scope.updateemail = currentUser.username;
            $scope.updatefname = currentUser.firstName;
            $scope.updatelname = currentUser.lastName;
            console.log("--> " + $scope.updateemail + " -- " + test);
        }
    }
});

和脚本文件 -

script(type="text/javascript", src="/vendor/jquery/dist/jquery.js")
script(type="text/javascript", src="/vendor/toastr/toastr.js")
script(type="text/javascript", src="/vendor/angular/angular.js")
script(type="text/javascript", src="/vendor/angular-resource/angular-resource.js")
script(type="text/javascript", src="/vendor/angular-route/angular-route.js")
script(type="text/javascript", src="/vendor/bootstrap/dist/js/bootstrap.js")
script(type="text/javascript", src="/vendor/bootstrap/js/dropdown.js")
script(type="text/javascript", src="/app/app.js")
script(type="text/javascript", src="/plugins/backstretch/jquery.backstretch.min.js")
script(type="text/javascript", src="/plugins/bxslider/jquery.bxslider.min.js")
script(type="text/javascript", src="/plugins/bxslider/jquery.bxslider.min.js")
script(type="text/javascript", src="/app/account/navBarLoginController.js")
script(type="text/javascript", src="/app/common/mvNotifier.js")
script(type="text/javascript", src="/app/account/mvIdentity.js")
script(type="text/javascript", src="/app/account/mvauth.js")
script(type="text/javascript", src="/app/account/mvUser.js")
script(type="text/javascript", src="/app/admin/mvUserListCtrl.js")
script(type="text/javascript", src="/app/account/mvSignupCtrl.js")
script(type="text/javascript", src="/app/account/mvProfileCtrl.js")  

1 个答案:

答案 0 :(得分:2)

这里的问题是,您正在尝试将控制器注入另一个控制器。那是它应该如何。

请查看:

要了解我们应该如何在控制器之间进行通信 - 使用共享服务。

服务和控制器之间的最大区别是(见Developer Guide / Dependency Injection):

  

控制器的特殊之处在于,与服务不同,应用程序中可能存在许多实例。例如,模板中的每个ng-controller指令都会有一个实例。

所以,当我们试图在另一个控制器中调用一个控制器的方法时......我们应该将它移到一个单独的服务中