AngularJS:使用共享服务(以$资源)共享控制器之间的数据,但如何界定回调函数? [英] AngularJS: Using Shared Service(with $resource) to share data between controllers, but how to define callback functions?
问题描述
请注意:我也贴在这里AngularJS邮件列表这个问题:<一href=\"https://groups.google.com/forum/#!topic/angular/UC8_pZsdn2U\">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有这么任何指导,开始将大大AP preciated:)
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,我从使用$资源对象的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.
这是code我收到了:
This was the code I had before:
CountryService:
CountryService:
services.factory('CountryService', function($resource) {
return $resource('http://localhost:port/restwrapper/client.json', {port: ':8080'});
});
CountryController:
CountryController:
//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:
CountryService:
CountryService:
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();
}
}
};
});
CountryController:
CountryController:
$scope.countries = CountryService.getCountries(function(result){
console.log("i need a callback function here...");
});
问题是,我曾经是能够使用$ resource.query回调函数(),以preSELECT默认的选择,但现在我搬到查询()我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?
感谢您的帮助,
肖恩
Thanks for your help, Shaun
推荐答案
Ok..so看起来像我传递一个回调函数一路攀升到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:
CountryController:
CountryController:
$scope.countries = CountryService.getCountries(function(){
//preselected default
$scope.selected.country = $scope.countries[0];
});
CountryService:
CountryService:
//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:使用共享服务(以$资源)共享控制器之间的数据,但如何界定回调函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!