每当将新对象添加到现有数组中时,我都想打印汽车列表。循环重复,我从头开始打印。我怎样才能避免这种情况?
I tried using async pipe and also track by
// in the service
getVehicles(){
obj = { data: [{name: 'car 1'},{name: 'car 2'}] }
return Observable.interval(2200).map(i=> obj.data.push({name: 'car 3'}));
}
// in the controller
vehicles: Observable<Array<any>>
ngOnInit() {
this.vehicles = this._vehicleService.getVehicles().obj.data;
}
// in template
<div *ngFor='let vehicle of vehicles | async'>
{{vehicle.name}}
</div>
预计 车 1 车 2 车 3 车 3
但它给了
车一车二车一车二车三车一车二车三车三