如何访问JSON对象。$$ state.value? [英] How to access JSON Object.$$state.value?
问题描述
警告:我要听起来像我不知道我在说什么在这里,因为我有点不知道。我在自学Javascript和AngularJS通过大量的试验和错误编码。过程
Warning: I'm going to sound like I have no idea what I'm talking about here, because I kind of don't. I'm in the process of self-learning Javascript and AngularJS through a lot of trial and error coding.
我有一些javascript code(犹豫,因为它是一个烂摊子复制到这里)返回具有以下结构的对象:
I have some javascript code (hesitant to copy here because it's a mess) that returns an Object with the following structure:
我要保存到一个变量是对应于对象的对象。$$ state.value的图片。这个对象有用户名,哈希和盐,这是我所关心的。我不知道是什么都喜欢$$州其他的东西是或者他们是如何到达那里。
What I want to save to a variable is the object corresponding to Object.$$state.value in the picture. This object has username, hash and salt, which are what I care about. I don't know what all the other stuff like $$state are or how they got there.
但是,如果我这样做(让我们把主要的对象whatIHave):
However, if I do this (let's call the main Object "whatIHave"):
var whatIWant = whatIHave.$$state.value;
这是行不通的。 whatIWant为null。
this does not work. whatIWant is null.
有谁认识这是怎么回事呢?什么是$$状态,它是怎么到达那里,我怎么能提取我想要的价值?
Does anyone recognize what's going on here? What is $$state, how did it get there, and how can I extract the value I want?
推荐答案
所以这是一个承诺。你需要做的是这样的:
So that is a promise. You need to do something like:
whatIHave.then(function(whatIWant) {
// Work here
});
我强烈建议你研究一个承诺是什么(这样链接一>)
如果你好奇的是,什么是 $$状态
什么是值
,我将解释了一下:
If you're curious enough, about what is that $$state
and what is that value
, I will explain a bit:
的承诺有一个 $$状态
有角可以节省你想在来调用待所有的回调函数
阵列(所有这些你。然后
注册功能就像我之前解释)。
The promises have a $$state
and there angular saves all the callback functions you want to call in a pending
array (all those function you registered with .then
like I explained before).
它还具有的状态:解决(1)和拒绝(2)
It also have the status: resolved (1) and rejected (2)
最后,当你决心
或拒绝
的承诺,你这样做时传递价值,保存在值
。
Finally, when you resolve
or reject
the promise, the value you pass when doing that, is saved in value
.
您正试图在这里欺骗的,因为当你尝试访问值
,也可能是现在还没有(这就是异步
是所有)。
You're trying to cheat in here, because when you try to access that value
, it could be not there yet (that is what async
is all about).
这样的想法是学习的承诺的基础上,学习如何与他们合作,然后用你的 whatIHave
正常。
So the idea is learning the basic of promises, learning how to work with them and then use your whatIHave
correctly.
这篇关于如何访问JSON对象。$$ state.value?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!