問題:
I am a bit confused about how to get the key
and value
of an object in angular2 while using *ngFor
for iterating over the object.我對如何在使用*ngFor
迭代對象時在 angular2 中獲取對象的key
和value
感到有些困惑。 I know in angular 1.x there is a syntax like我知道在 angular 1.x 中有類似的語法
ng-repeat="(key, value) in demo"
but I don't know how to do the same in angular2.但我不知道如何在 angular2 中做同樣的事情。 I have tried something similar, without success:我嘗試過類似的東西,但沒有成功:
<ul>
<li *ngFor='#key of demo'>{{key}}</li>
</ul>
demo = {
'key1': [{'key11':'value11'}, {'key12':'value12'}],
'key2': [{'key21':'value21'}, {'key22':'value22'}],
}
Here is a plnkr with my attempt: http://plnkr.co/edit/mIj619FncOpfdwrR0KeG?p=preview這是我嘗試的 plnkr: http ://plnkr.co/edit/mIj619FncOpfdwrR0KeG?p=preview
How can I get key1
and key2
dynamically using *ngFor
?如何使用*ngFor
動態獲取key1
和key2
? After searching extensively, I found the idea of using pipes but I don't know how to go about it.在廣泛搜索後,我發現了使用管道的想法,但我不知道如何去做。 Is there any inbuilt pipe for doing the same in angular2?是否有任何內置管道可以在 angular2 中做同樣的事情?
解決方案:
參考一: https://stackoom.com/question/2P6GV參考二: access key and value of object using *ngFor