问题描述
我有这样的工厂:
.factory("Member", function($resource) {
var endpoint = "http://some valid url";
function generateMemberToken (id1, id2) {
return $resource(endpoint, null, {
query: {
method: 'GET',
headers: {
'id1': id1,
'id2': id2
}
}
})
}
var member = {
generateMemberToken : generateMemberToken
}
return member;
})
和该控制器:
.controller('memberKeyCtrl', function($scope, Member){
$scope.generateMemberToken = function () {
$scope.member = Member.generateMemberToken('123456789', '789456123').query();
console.log($scope.member);
console.log($scope.member.someProperty); // problem here
}
$ scope.member是一个API JSON响应,即
$scope.member is a JSON response from an API, i.e.
{firstName:Joe, lastName:smith}
我可以看到在控制台$ scope.member我的输出中,它有物业即时寻找在这里,被称为.someProperty。我似乎无法提取数据,因为我有在这里,看到在控制器中的//这里的问题行。
I can see my ouput in the console for "$scope.member" and it has the property im looking for here, referred to as .someProperty. I just cannot seem to extract that data as i have it here, see the "//problem here" line in the controller.
我的猜测是,不知何故,我设置了我厂的不正确或我不是做房地产莫名其妙可用在工厂外部。新角所以遇到一些麻烦理解这一点。
My guess is that I somehow set up my factory incorrectly or am not making the property somehow available outside of the factory. New to angular so having some trouble understanding this.
请让我知道是否需要任何额外的信息。
Please let me know if any additional info is needed.
推荐答案
这里的问题是$资源的异步性,并在控制台如何与数据进行交互。
The issue here is with the Async nature of $resource, and how the console interacts with your data.
.query()
是一个异步调用,这意味着它不立即返回一个值;相反,它允许code继续流动。你的下一个语句是的console.log($ scope.member);
,它不会立即展开 $ scope.member
来看看它的属性,直到它们被使用或检查。接下来的调用是的console.log($ scope.member.someProperty);
,其中会展开 $范围。成员
,以便找到 someProperty
。由于回调到你的 .query()
还没有恢复,这是要登录未定义
。通过你去检查,看看是什么在第一个的的console.log
,回调已经完成,所以对象看起来好像时间它有所有属性一直以来,尽管它并没有真正,当时他们试图访问。
.query()
is an asynchronous call, which means that it does not return a value immediately; instead it allows the code to continue to flow. Your next statement is to console.log($scope.member);
, which doesn't immediately expand $scope.member
to see it's properties until they are used or inspected. The next call is to console.log($scope.member.someProperty);
, which would expand $scope.member
in order to find someProperty
. Since the callback to your .query()
hasn't returned yet, this is going to log undefined
. By the time you go to check to see what is in the first console.log
, the callback has completed, and so the object appears as though it had all the properties all along, even though it didn't really, at the time they were attempted to be accessed.
最简单的方式来处理,这是包装在。然后调用()
,以确保您不会执行,直到承诺之后,从 .query()返回
。为了链条。然后()
,您需要访问返回的承诺,即
The simple way to handle this is to wrap the calls in .then()
, to ensure that you aren't executing them until after the promise from .query()
has returned. In order to chain .then()
, you need to access the promise returned, i.e.
$scope.generateMemberToken = function () {
$scope.member = Member.generateMemberToken('123456789', '789456123')
.query().$promise.then(function(){
console.log($scope.member);
console.log($scope.member.someProperty); // problem here
});
};
的 .query()
函数语法允许匿名回调,这将允许你传递应执行的功能,当查询()
完成,而不是链接。然后()
语句。例如:
The .query()
function syntax allows for an anonymous callback, that will allow you to pass a function that should be executed when the query()
is complete, rather than chaining .then()
statements. For example:
$scope.generateMemberToken = function () {
$scope.member = Member.generateMemberToken('123456789', '789456123')
.query({}, function() {
console.log($scope.member);
console.log($scope.member.someProperty); // problem here
});
}
这篇关于获得从角工厂$资源响应数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!