我有一个组件是root的子组件,组件有1个属性,当behaviourSubject更改时会更新。视图循环遍历此属性,在页面上显示结果。
问题是如果我使用this.changeDetector.detectChanges(),组件不会显示列表。如果我删除此行,我可以看到列表,但是当我更改列表的排序顺序时,视图不会更改。
import {Component, OnInit, Input, ChangeDetectorRef} from '@angular/core';
import {MapLocation} from "../models/location";
import {MapStoreService} from "../store/location.store";
@Component({
selector: 'app-location-list',
templateUrl: './location-list.component.html',
styleUrls: ['./location-list.component.css']
})
export class LocationListComponent implements OnInit {
private locationList: MapLocation[];
constructor(private changeDetector: ChangeDetectorRef,private mapStoreService: MapStoreService) {
this.mapStoreService.publicLocationList.subscribe(objList => {
this.locationList = objList;
this.triggerChanges();
});
}
ngOnInit() { }
triggerChanges(){
this.changeDetector.detectChanges();
}
}
地图服务会更改排序顺序,然后更新商店。 提前谢谢。