对于如何在angular2中使用*ngFor迭代对象时获取对象的键和值,我有点困惑。我知道角1。X有一个语法
ng-repeat="(key, value) in demo"
但是我不知道如何在angular2中做同样的事情。我也做过类似的尝试,但没有成功:
<ul>
<li *ngFor='#key of demo'>{{key}}</li>
</ul>
demo = {
'key1': [{'key11':'value11'}, {'key12':'value12'}],
'key2': [{'key21':'value21'}, {'key22':'value22'}],
}
以下是我的尝试:
http://plnkr.co/edit/mIj619FncOpfdwrR0KeG?p=preview
如何使用*ngFor动态获取key1和key2 ?经过广泛的搜索,我发现了使用管道的想法,但我不知道如何去做。
在angar2中有任何内建的管道来做同样的事情吗?
和Angular的最新版本(v6.1.0)一样,Angular团队在Angular package的公共模块中添加了与keyvalue管道同名的内置管道,以帮助你迭代对象、映射和数组。
例如:
<div *ngFor="let item of testObject | keyvalue">
Key: <b>{{item.key}}</b> and Value: <b>{{item.value}}</b>
</div>
要保持原来的顺序,使用keyvalue:onCompare,
在组件中定义回调:
// ...
import {KeyValue} from '@angular/common';
@Component(/* ... */)
export class MyComponent {
private onCompare(_left: KeyValue<any, any>, _right: KeyValue<any, any>): number {
return -1;
}
}
工作分叉示例
点击这里查看更多有用的信息
https://github.com/angular/angular/blob/master/CHANGELOG.md#features-3
https://github.com/angular/angular/commit/2b49bf7
如果你使用的是Angular v5或更低版本,或者你想使用管道实现,请遵循这个答案
使用ngfor获取对象的访问键和值
@Marton对接受的答案有一个重要的反对意见,理由是管道在每次更改检测时都会创建一个新的集合。相反,我将创建一个HtmlService,它提供了一系列实用函数,视图可以使用如下:
@Component({
selector: 'app-myview',
template: `<div *ngFor="let i of html.keys(items)">{{i + ' : ' + items[i]}}</div>`
})
export class MyComponent {
items = {keyOne: 'value 1', keyTwo: 'value 2', keyThree: 'value 3'};
constructor(private html: HtmlService){}
}
@Injectable()
export class HtmlService {
keys(object: {}) {
return Object.keys(object);
}
// ... other useful methods not available inside html, like isObject(), isArray(), findInArray(), and others...
}
更新
在6.1.0-beta.1KeyValuePipe是https://github.com/angular/angular/pull/24319引入的
<div *ngFor="let item of {'b': 1, 'a': 1} | keyvalue">
{{ item.key }} - {{ item.value }}
</div>
砰砰作响的例子
之前的版本
另一种方法是创建NgForIn指令,如下所示:
<div *ngFor="let key in obj">
<b>{{ key }}</b>: {{ obj[key] }}
</div>
砰砰作响的例子
ngforin.directive.ts
@Directive({
selector: '[ngFor][ngForIn]'
})
export class NgForIn<T> extends NgForOf<T> implements OnChanges {
@Input() ngForIn: any;
ngOnChanges(changes: NgForInChanges): void {
if (changes.ngForIn) {
this.ngForOf = Object.keys(this.ngForIn) as Array<any>;
const change = changes.ngForIn;
const currentValue = Object.keys(change.currentValue);
const previousValue = change.previousValue ? Object.keys(change.previousValue) : undefined;
changes.ngForOf = new SimpleChange(previousValue, currentValue, change.firstChange);
super.ngOnChanges(changes);
}
}
}
ECMA 2017+解决方案
遵循一些其他答案的思想,您可以创建一个Pipe来从您的对象创建一个[key, value]数组,但是以一种更简单的方式,遵循新的方法object。在ECMA 2017中引入的条目。
Pipe
import { PipeTransform, Pipe } from '@angular/core';
/**
* Transform a literal object into array
*/
@Pipe({
name: 'forObject',
pure: true,
})
export class ForObjectPipe implements PipeTransform {
transform(object, args?: any): any {
return Object.entries(object);
}
}
模块
在模块中,声明并提供它
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ForObjectPipe } from './for-object.pipe';
import { MyPageRoutingModule } from './my-routing.module';
import { MyPage } from './my.page';
@NgModule({
imports: [
CommonModule,
MyPageRoutingModule,
],
declarations: [
MyPage,
ForObjectPipe,
],
providers: [
ForObjectPipe,
]
})
export class MyPageModule {}
然后你可以在你的组件中使用typescript代码或HTML。
组件中使用
// ...
import { ForObjectPipe } from './for-object.pipe';
@Component({
selector: 'app-my',
templateUrl: './my.page.html',
styleUrls: ['./my.page.scss'],
})
export class MyComponent {
obj: { [key: any]: any } = {
1: 'hello',
2: 'world',
};
constructor(private forObjectPipe: ForObjectPipe) { }
foo() {
const myArray = this.forObjectPipe.transform(this.obj);
// same as
const myArray = Object.entries(this.obj);
}
}
在组件视图中使用
<h1>Object:</h1>
<div *ngFor="let pair of obj | forObject">
KEY: {{ pair[0] }} - VALUE: {{ pair[1] }}
</div>
输出:
Object:
KEY: 1 - VALUE: hello
KEY: 2 - VALUE: world
现场演示:https://stackblitz.com/edit/angular-qapapx?file=src/app/hello.component.ts