using factory in angularjs getting Error: [$injector:undef]

I am creating a new angular App and using factory but when i am am getting an error which is

Error: [$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 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) at https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js:40:410 at d (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js:38:308) at e (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js:39:64) at 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) at 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');
                }
                });
            };

        }]);


Factory functions in angular are expected to return an object, but your "Data" factory does not return anything. Simply add the following at the end of your factory function to fix the issue:

return obj;

Have a look at the link provided in your stacktrace. Your factory has to return a value:

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

There are many error, in controller your are using $http, what is the purpose

You are calling init() inside the init which will cause the infinite loop

You service is not returning anything

I will suggest to check the basics angular first

链接地址: http://www.djcxy.com/p/77610.html

上一篇: 从方法中读取AngularJS中的$ Cookies

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