在我的角度项目中,我有以下几点:角对象不确定
TS
this.storage.get(item_section).then((value) => {
this.item= value;
console.log(this.item); //The console log gives `["name","item","size"]`
console.log(this.item[1]); //Gives `item` as the console log
});
HTML
<div class="something">{{item}}</div> //Displays "name,item,size"
<div class="something">{{item[1]}}</div> //Gets Undefined error
如果我可以定义this.item
并获得结果{{item}}
,{{item[1]}}
怎么会出现undefined错误?
我有点困惑如何解决这个问题
你的代码异步的。试想一下 – yurzui
'item'只能在异步操作完成时才能分配。你可以在组件中使用这样的东西:'this.item = this.storage.get(item_section);'和in template:'{{resolvedItem[1]}} ng-container >' –
developer033
如果你想知道它为什么发生,你可以看到[**这个问题**](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-afteri-i-修改 - 它-里面对的一功能-asynchron#23667087)。 – developer033