这可能是有史以来最愚蠢的问题,但请记住,我对Angular2非常新。
在Angular2中,如何在每个循环后如何制作值渲染。我不想使用 *ngfor。只是这样的循环
@Component({
templateUrl: 'app/categories/categories.component.html'
})
ngOnInit(){
export class CategoriesComponent implements OnInit {
for(var i=0; i<this.categories.length; i++) {
count=i;
}
}
}
每个循环更新HTML。
Loop number {{ count }}
谢谢。
我不明白这是什么用例,但是您可以去:
categories.component.ts
import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
@Component({
templateUrl: 'app/categories/categories.component.html'
})
export class CategoriesComponent implements OnInit {
private count: number;
constructor(private changeDetectorRef: ChangeDetectorRef) { }
ngOnInit(){
for(var i=0; i<this.categories.length; i++) {
this.count=i;
this.changeDetectorRef.detectChanges();
}
}
}
categories.component.html
<div>Loop number {{ count }}</div>
您不应该这样做。为什么不使用*ngFor
?