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

时间:2015-10-01 14:33:13

标签: angularjs factory angular-services

我正在创建一个新的角度App并使用工厂,但是当我收到错误

错误:[$ injector:undef] http://errors.angularjs.org/1.4.5/ $ injector / undef?p0 =数据     在错误(本机)     在https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js:6:416     at Object。$ get(https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js:37:32)     at 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)     at 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',
          function ($routeProvider) {
                $routeProvider.
                when('/userlist', {
                    title: 'List of all the users',
                    templateUrl: 'userlist.html',
                    controller: 'listuser'
                })
                .when('/profile',{
                    title:'user detail page',
                    templateUrl:'profile.html',
                    controller:'listuser'
                })
                .otherwise({
                    redirectTo: '/userlist'
                });
        }])

        app.factory("Data", ['$http','$log',
        function ($http,$log) {
            var obj = {};

            obj.post = function (q, object) {
                return $http({
                     method: 'POST',
                     url:'userdetail.php',

                     data: object.user

                })
                .then(function (results) {
                    $log.log(results);
                    return results.data;
                });

            };
        }]);

        app.controller('listuser',['Data', function ($scope,$http,$log,$window,Data) {
            $scope.userdetail = {};
            var init = function () {
            $http({
                     method: 'POST',
                     url: 'apisource.php',

                })
                .then(function (results) {

                    $scope.data=results.data;

                });
            };
            init();
             $scope.douserdetail = function(user) {
                Data.post({
                    user: user
                }).then(function (results) {

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

        }]);

3 个答案:

答案 0 :(得分:3)

有角度的工厂函数应返回一个对象,但您的"Data"工厂不会返回任何内容。只需在工厂功能结束时添加以下内容即可解决问题:

return obj;

答案 1 :(得分:0)

查看堆栈跟踪中提供的link。您的工厂必须返回一个值:

app.factory("Data", ['$http','$log',
  function ($http,$log) {
    var obj = {};
    ...
    return obj;
  }]);    

答案 2 :(得分:0)

有许多错误,在控制器中你使用$ http,目的是什么

你在init中调用init()会导致无限循环

您的服务不会返回任何内容

我建议首先检查基本角度