4

我有一个通过 REST 检索数据的服务。我想将结果数据存储在服务级别变量中,以便在多个控制器中使用。当我将所有 REST 逻辑直接放入控制器时,一切正常,但是当我尝试将数据的检索/存储移动到服务中时,当数据返回时控制器不会被更新。我尝试了很多不同的方法来维护服务和控制器之间的绑定。

控制器:

myApp.controller('SiteConfigCtrl', ['$scope', '$rootScope', '$route',  'SiteConfigService',
function ($scope, $rootScope, $route, SiteConfigService) {

    $scope.init = function() {
        console.log("SiteConfigCtrl init");
        $scope.site = SiteConfigService.getConfig();
    }

}

]);

服务:

 myApp.factory('SiteConfigService', ['$http', '$rootScope', '$timeout', 'RESTService',
 function ($http, $rootScope, $timeout, RESTService) {

    var siteConfig = {} ;

    RESTService.get("https://domain/incentiveconfig", function(data) {
        siteConfig = data;
    });

    return {

        getConfig:function () {
            console.debug("SiteConfigService getConfig:");
            console.debug(siteConfig);

            return siteConfig;
        }

     };
 }
]);

看法:

<div class="span4" ng-controller="SiteConfigCtrl">
            <header>
                <h2>
                    {{site.title}}
                </h2>
            </header>
4

2 回答 2

5

我会用承诺工厂写它:

myApp.factory('SiteConfigService', ['$http', '$rootScope', '$timeout', 'RESTService', '$q'
 function ($http, $rootScope, $timeout, RESTService, $q) {

    var siteConfig = {} ;

    RESTService.get("https://domain/incentiveconfig", function(data) {
        siteConfig = data;
    });

  // or just 
  // var siteConfig = RESTService.get("https://domain/incentiveconfig");

    return {

        getConfig:function () {
             var deferred = $q.defer();
             deferred.resolve(siteConfig);

              return deferred.promise;    
        }

     };
 }
]);

控制器端

         SiteConfigService.getConfig()
                    .then(function (result) {
                       $scope.site = result;                           
                    }, function (result) {
                        alert("Error: No data returned");
                    });
于 2013-10-28T22:17:06.343 回答
2

基于上述Maxim答案的解决方案 - JsFiddle - http://jsfiddle.net/acb98sm/2pQ6A/6/

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

myApp.controller('SiteConfigCtrl', ['$scope', '$rootScope', '$route', 'SiteConfigService', 
function ($scope, $rootScope, $route, SiteConfigService) {

    SiteConfigService.getConfig()
            .then(function (result) {
                console.log("results are in ");
                console.log(result); 
               $scope.site = result.data; 

            }, function (result) {
                alert("Error: No data returned");
            });

}

]);

myApp.factory('SiteConfigService', ['$http', '$rootScope', '$timeout', 'RESTService', '$q',
 function ($http, $rootScope, $timeout, RESTService, $q) {

 var siteConfigFn = RESTService.get("http://graph.facebook.com/616366118/",    function(data) {
    console.log("SiteConfigService returns");
});

return {

    getConfig:function () {
         var deferred = $q.defer();
         deferred.resolve(siteConfigFn);

          return deferred.promise;    
    }

 };
 }
]);

 myApp.$inject = ['$scope', 'SiteConfigService', 'RESTService'];

 myApp.factory('RESTService',
 function ($http) {
    return {
        get:function (url, callback) {
            return $http.get(url, {withCredentials:false}).
                success(function (data, status, headers, config) {
                    callback(data);
                }).
                error(function (data, status, headers, config) {
                    console.log("failed to retrieve data");
                });
        },
        post:function (url, data, callback) {
            return $http.post(url, data, {withCredentials:true}).
                success(function (data, status, headers, config) {
                    callback(data);
                }).
                error(function (data, status, headers, config) {
                    console.log("failed to retrieve data");
                });
        }
    };
  }
);
于 2013-10-28T23:29:03.180 回答