AngularJS:使用共享服务(带 $resource)在控制器之间共享数据,但如何定义回调函数? [英] AngularJS: Using Shared Service(with $resource) to share data between controllers, but how to define callback functions?

查看:21
本文介绍了AngularJS:使用共享服务(带 $resource)在控制器之间共享数据,但如何定义回调函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我也在此处的 AngularJS 邮件列表上发布了这个问题:https://groups.google.com/forum/#!topic/angular/UC8_pZsdn2U

Note: I also posted this question on the AngularJS mailing list here: https://groups.google.com/forum/#!topic/angular/UC8_pZsdn2U

大家好,

我正在构建我的第一个 AngularJS 应用程序,一开始我对 Javascript 不太熟悉,因此非常感谢任何指导:)

I'm building my first AngularJS app and am not very familiar with Javascript to begin with so any guidance will be much appreciated :)

我的应用程序有两个控制器,ClientController 和 CountryController.在 CountryController 中,我从使用 $resource 对象的 CountryService 中检索国家/地区列表.这工作正常,但我希望能够与 ClientController 共享国家/地区列表.经过一番研究,我了解到我应该使用 CountryService 来存储数据并将该服务注入到两个控制器中.

My App has two controllers, ClientController and CountryController. In CountryController, I'm retrieving a list of countries from a CountryService that uses the $resource object. This works fine, but I want to be able to share the list of countries with the ClientController. After some research, I read that I should use the CountryService to store the data and inject that service into both controllers.

这是我之前的代码:

国家服务:

services.factory('CountryService', function($resource) {
    return $resource('http://localhost:port/restwrapper/client.json', {port: ':8080'});
});

国家控制器:

//Get list of countries         
//inherently async query using deferred promise
$scope.countries = CountryService.query(function(result){       
        //preselected first entry as default
    $scope.selected.country = $scope.countries[0];  
});

在我更改之后,它们看起来像这样:

And after my changes, they look like this:

国家服务:

services.factory('CountryService', function($resource) {
    var countryService = {};

    var data;
    var resource = $resource('http://localhost:port/restwrapper/country.json', {port: ':8080'});

    var countries = function() {
        data = resource.query();
        return data;
    }

    return {
        getCountries: function() {
            if(data) {
                console.log("returning cached data");
                return data;
            } else {
                console.log("getting countries from server");
                return countries(); 
            }

        }
    };
  });

国家控制器:

$scope.countries = CountryService.getCountries(function(result){
        console.log("i need a callback function here...");
});

<小时>

问题是我曾经能够使用 $resource.query() 中的回调函数来预选默认选择,但是现在我已经将 query() 调用移动到我的 CountryService 中,我似乎失去了什么.


The problem is that I used to be able to use the callback function in $resource.query() to preselect a default selection, but now that I've moved the query() call to within my CountryService, I seemed to have lost what.

解决此问题的最佳方法是什么?

What's the best way to go about solving this problem?

感谢您的帮助,肖恩

推荐答案

好吧..所以看起来我通过传递一个回调函数一直到 resource.query() 调用解决了这个问题.仍然不确定这是否是最好的方法.

Ok..so looks like I solved the problem by passing a callback function all the way up to the resource.query() call. Still not sure if this is the best way to do this.

作为参考,这是我所做的:

For reference, this is what I did:

国家控制器:

$scope.countries = CountryService.getCountries(function(){
    //preselected default
    $scope.selected.country = $scope.countries[0];  
});

国家服务:

//Country Service. Will contain all relevant rest methods here
services.factory('CountryService', function($resource) {
    var countryService = {};

    var data;
    var resource = $resource('http://localhost:port/restwrapper/country.json', {port: ':8080'});

    var countries = function(callback) {
        data = resource.query(callback);
        return data;
    }


    return {
        getCountries: function(callback) {
            if(data) {
                console.log("returning cached data");
                return data;
            } else {
                console.log("getting countries from server");
                return countries(callback); 
            }

        }
    };
  });

这篇关于AngularJS:使用共享服务(带 $resource)在控制器之间共享数据,但如何定义回调函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆