Javascript 访问angularjs中的私有$$状态变量
我正在尝试通过http请求访问多个用户。 但所需的数据嵌套在私有$$状态变量中,我无法访问该变量Javascript 访问angularjs中的私有$$状态变量,javascript,html,angularjs,http,Javascript,Html,Angularjs,Http,我正在尝试通过http请求访问多个用户。 但所需的数据嵌套在私有$$状态变量中,我无法访问该变量 如果您得到的是$$state对象,则可能是在编写时直接访问从getMembers()函数返回的承诺。记住,承诺是异步的。要访问承诺中的数据,需要使用回调函数在承诺返回时访问数据 错误示例: response = getMembers(circleId); console.log(response); // the data is NOT in this response 正确的方法:
如果您得到的是$$state对象,则可能是在编写时直接访问从
getMembers()
函数返回的承诺。记住,承诺是异步的。要访问承诺中的数据,需要使用回调函数在承诺返回时访问数据
错误示例:
response = getMembers(circleId);
console.log(response); // the data is NOT in this response
正确的方法:
getMembers(circleId).then(function(response) {
console.log(response); // the data IS in this response
$scope.data = response.data; // or something like that
});
如果您得到的是$$state对象,则可能是在编写时直接访问从
getMembers()
函数返回的承诺。记住,承诺是异步的。要访问承诺中的数据,需要使用回调函数在承诺返回时访问数据
错误示例:
response = getMembers(circleId);
console.log(response); // the data is NOT in this response
正确的方法:
getMembers(circleId).then(function(response) {
console.log(response); // the data IS in this response
$scope.data = response.data; // or something like that
});
演示如何调用
getMembers()
访问数据。注意使用$q.defer()
是一种反模式,因为$http
方法已经返回了一个promiseShow,告诉我们如何调用getMembers()
来访问数据。注意使用$q.defer()
是一种反模式,因为$http
方法已经返回承诺