问题:
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