在AngularJS SPA使用ngResource [英] Using ngResource in an AngularJS SPA
问题描述
我使用ngResource在一家工厂获取从REST API URL数据,然后做检索到的数据的一些基本处理。这工作正常(使用验证的console.log()
)。我中喷射出的依赖关系正确我根模块,并在主控制器,并设置一个 $范围
变量等于该检索到的数据。
I am using ngResource in a factory to fetch data from a REST API URL, and then do some basic processing on the retrieved data. This works fine (verified using console.log()
). I injected the dependencies properly in my root module and in the main controller, and set a $scope
variable equal to this retrieved data.
在HTML中,我把 {{} VARIABLENAME}
所需的标签内。
但是,它并没有得到填充。
In HTML, I put {{variableName}}
inside the desired tag.
However, it doesn't get populated.
我在想什么?我是否需要添加 $观看
或别的东西,为了这个工作?
What am I missing? Do I need to add $watch
or something else in order for this to work?
PS -
VAR数据资源= $资源(HTTP://< URL>中);
$ scope.trialData = dataResource.get();
本 trialData code>从同一个网址,当我添加此直接向控制器填充值就好了。
This trialData
populates values from the same URL just fine when I add this directly to the controller.
推荐答案
资源使用异步请求。你必须等待他们的承诺,这样解决:
Resources use asynchronous request. You have to wait for their promise to be resolved like this :
dataResource.get().$promise.then(function(object){
$scope.trialData = object;// or maybe object.data
});
这篇关于在AngularJS SPA使用ngResource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!