在 angularjs 中使用工厂获取错误:[$injector:undef]

Gau*_*ert -1 factory angularjs angular-services

我正在创建一个新的 angular 应用程序并使用工厂,但是当我收到一个错误时

错误:[$injector:undef] http://errors.angularjs.org/1.4.5/ $injector/undef?p0=Data at Error (native) at https://ajax.googleapis.com/ajax/libs/ angularjs/1.4.5/angular.min.js:6:416 在 Object.$get ( https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js:37:32 ) 在 Object.e [as invoke] ( https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js:39:96 ) 在https://ajax.googleapis.com /ajax/libs/angularjs/1.4.5/angular.min.js:40:410 在 d ( https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js:38 :308 ) 在 e ( https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js:39:64) 在 Object.g.instantiate ( https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js:39:213 ) 在 b.$get ( https://ajax. googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js:80:257 ) 在 s ( https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0rc1/angular-route .min.js:12:165 )

    var app = angular.module('testapp', ['ngRoute']);
        app.config(['$routeProvider',
          功能($routeProvider){
                $routeProvider.
                当('/用户列表',{
                    title: '所有用户列表',
                    templateUrl: 'userlist.html',
                    控制器:'列表用户'
                })
                .when('/个人资料',{
                    title:'用户详情页面',
                    templateUrl:'profile.html',
                    控制器:'listuser'
                })
                。除此以外({
                    重定向到:'/用户列表'
                });
        }])

        app.factory("数据", ['$http','$log',
        函数($http,$log){
            var obj = {};

            obj.post = 函数(q,对象){
                返回 $http({
                     方法:'POST',
                     url:'userdetail.php',

                     数据:object.user

                })
                .then(函数(结果){
                    $log.log(结果);
                    返回结果数据;
                });

            };
        }]);

        app.controller('listuser',['Data', function ($scope,$http,$log,$window,Data){
            $scope.userdetail = {};
            var init = 函数 () {
            $http({
                     方法:'POST',
                     网址:'apisource.php',

                })
                .then(函数(结果){

                    $scope.data=results.data;

                });
            };
            在里面();
             $scope.douserdetail = 函数(用户){
                数据.post({
                    用户:用户
                }).then(函数(结果){

                if (results.status == "success") {
                    //$location.path('dashboard');
                }
                });
            };

        }]);

pla*_*mut 5

angular 的工厂函数应该返回一个对象,但你的"Data"工厂不返回任何东西。只需在工厂函数的末尾添加以下内容即可解决问题:

return obj;
Run Code Online (Sandbox Code Playgroud)