2

试图从工厂返回数据并在工厂内记录输出正确的数据,但一旦传递给控制器​​,它总是未定义的。如果我在控制器中有我的工厂逻辑,它将正常工作。所以这一定是我在这里缺少的一些简单的东西?

应用

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

    app.controller('animalController', ['$log', '$scope', 'animalResource', function($log, $scope, animalResource) {
        $scope.list = function() {
            $scope.list = 'List Animals';
            $scope.animals = animalResource.get(); // returns undefined data
            $log.info($scope.animals);
        };
        $scope.show = function() {};
        $scope.create = function() {};
        $scope.update = function() {};
        $scope.destroy = function() {};
    }]);

    app.factory('animalResource', ['$http', '$log', function($http, $log) {
        return {
            get: function() {
                $http({method: 'GET', url: '/clusters/xhrGetAnimals'}).
                    success(function(data, status, headers, config) {
                        //$log.info(data, status, headers, config); // return correct data
                        return data;
                    }).
                    error(function(data, status, headers, config) {
                        $log.info(data, status, headers, config);
                    });
            },
            post: function() {},
            put: function() {},
            delete: function() {}
        };
    }]);

日志信息

[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
    200 function (name) {
        if (!headersObj) headersObj =  parseHeaders(headers);

        if (name) {
            return headersObj[lowercase(name)] || null;
        }

        return headersObj;
    } Object {method: "GET", url: "/clusters/xhrGetAnimals"} 
4

2 回答 2

3

get()的服务方法没有返回任何内容。return内部success回调仅从该特定函数返回。

返回$http对象

于 2013-04-01T01:06:14.197 回答
0

Che这个例子是你如何使用承诺并返回工厂然后你访问在你的控制器上注入服务的方法使用点语法访问你在服务上定义的函数

'use strict';
        var app;

        app = angular.module('app.formCreator.services', []);

        app.factory('formCreatorService', [
          '$http', '$q', function($http, $q) {
            var apiCall, bjectArrarContainer, deferred, factory, webBaseUrl, _getFormElementsData;
            factory = {};
            deferred = $q.defer();
            bjectArrarContainer = [];
            webBaseUrl = 'https://tools.XXXX_url_XXXXX.com/XXXXXXX/';
            apiCall = 'api/XXXXX_url_XXXX/1000';
            _getFormElementsData = function() {
              $http.get(webBaseUrl + apiCall).success(function(formElements) {
                deferred.resolve(formElements);
              }).error(function(err) {
                deferred.reject(error);
              });
              return deferred.promise;
            };
            factory.getFormElementsData = _getFormElementsData;
            return factory;
          }
        ]);

然后例如这样做

 'use strict';
            var app;

            app = angular.module('app.formCreator.ctrls', []);

            app.controller('formCreatorController', [
              'formCreatorService', '$scope', function(formCreatorService, $scope) {
                $scope.formElementsData = {};
                formCreatorService.getFormElementsData().then(function(response) {
                  return $scope.formElementsData = response;
                });
              }
            ]);
于 2015-03-11T19:06:22.087 回答