Angularjs错误未知提供者:$ scopeProvider < - $ scope < - user

sib*_*e94 3 javascript angularjs angularjs-factory angular-controller

我收到了错误

未知提供商:$ userProvider < - $ user"

使用以下代码:

var app = angular.module('test', []);

app.factory("user", function($scope, $http) {
   var usr = {};
   usr.getAllLists = function(){
       return "test";
   }
   return usr;  
});

cart.controller("MyController", ["$scope", "$http", "$user", 
    function ($scope, $http, user){

        $scope.initialize = function(){
            $scope.lists = user.getAllLists();
        }

    }
]);
Run Code Online (Sandbox Code Playgroud)

你看错了吗?

Nik*_*wal 6

假设购物车依赖于app模块.

cart.controller("MyController", ["$scope", "$http", "user", 
    function ($scope, $http, user){

        $scope.initialize = function(){
            $scope.lists = user.getAllLists();
        }
    }
Run Code Online (Sandbox Code Playgroud)

它必须是用户而不是$user.

另外,在工厂,代替$scope,使用$rootScope.

  • 我将工厂的$ scope更改为$ rootScope.现在它有效.控制器是否有自己的范围? (2认同)