AngularJS资源承诺 [英] AngularJS resource promise
本文介绍了AngularJS资源承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经得到了使用$资源一个简单的控制器:
VAR地区= $资源('嘲笑/ regions.json'); $ scope.regions = Regions.query();
我用了这个指令控制器(在链接功能)
VAR地区= scope.regions;
但地区处于不确定状态。这是pretty逻辑调用是异步的。
我的问题是我该怎么做才能等到的结果和地区与所有数据的数组?
更新:
该指令在这里的定义
app.directive('ngMap',函数(){
返回{
限制:EA,
替换:'真',
范围: { },
模板:'< DIV ID =地图>< / DIV>,
控制器:'AccordMapCtrl',
链接:功能(范围,元素,ATTRS){
VAR地区= scope.regions;
的console.log(地区); 对(在区域变种区域){}
};
});
解决方案
如果你想使用你需要$承诺使用回调函数异步方法,我这里是:
VAR地区= $资源('嘲笑/ regions.json');$ scope.regions = Regions.query();
$ scope.regions。$ promise.then(功能(结果){
$ scope.regions =结果;
});
I've got a simple controller that use $resource :
var Regions = $resource('mocks/regions.json');
$scope.regions = Regions.query();
I'm using this controller in a directive (in the link function)
var regions = scope.regions;
But regions is undefined. It's pretty logic the call is asynchronous.
My question is how can i do to wait the result and regions be an array with all data ?
UPDATE :
Here the definition of the directive
app.directive('ngMap', function() {
return {
restrict: 'EA',
replace: 'true',
scope: {
},
template: '<div id="map"></div>',
controller: 'AccordMapCtrl',
link: function(scope, element, attrs) {
var regions = scope.regions;
console.log(regions);
for (var region in regions) {}
};
});
解决方案
If you want to use asynchronous method you need to use callback function by $promise, here is example:
var Regions = $resource('mocks/regions.json');
$scope.regions = Regions.query();
$scope.regions.$promise.then(function (result) {
$scope.regions = result;
});
这篇关于AngularJS资源承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文