每个循环后显示angular2

时间:2016-12-23 11:38:05

标签: angular

这可能是有史以来最愚蠢的问题,但请记住,我对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 }}

谢谢。

1 个答案:

答案 0 :(得分:0)

我不明白这是什么用例,但是你走了:

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