我尝试了this answer和this one,但我无法让它发挥作用。
我有这个:
HTML:
<span *ngFor="let block of blocks; let i = index; trackBy: trackByFn">
{{block.description}}:
</span>
组件:
@Component({
moduleId: module.id,
styleUrls: [ 'component.css' ],
templateUrl: 'component.html'})
export class UserComponent implements OnInit {
blocks: any = [];
constructor(private userService: UserService) {}
ngOnInit(): void {
this.loadCourse();
}
private loadCourse(){
this.http.get(url + parseInt(localStorage.getItem('id'))).map((response: Response) => response.json())
.subscribe(resp => {
this.blocks = resp.blocks;
});
}
trackByFn(index, item) {
return index;
}
然后,在一些用户交互之后,我触发了这两行:
localStorage.setItem('id','4');
this.loadCourse();
并且块数组已更新,但DOM未更新,因此我没有在HTML中看到新描述。
缺少什么?
谢谢!