ngFor自定义组件

时间:2018-09-03 09:28:39

标签: angular ionic3 components ngfor

我正在处理Ionic v3中的自定义组件,并且正在尝试使用ngFor。

在我的.ts文件中,我在[0, 0, 0, 0]中有此内容:

ngOnInit

在我的.html文件中,这是

this.nb_stars = [];

for(let i=1; i<=4; i++) {
   this.nb_stars.push(i);
}

但是我有这个错误:

  

未捕获的错误:模板解析错误:由于以下原因无法绑定到“ ngForIn”   它不是'div'的已知属性。 (“         ->] * ngFor =“在nb_stars中进行测试”>

     

“):ng:///ComponentsModule/CompStarsComponent.html@3:5属性   嵌入式模板上的任何指令都没有使用绑定ngForIn。

     

确保属性名称拼写正确且所有   指令在“ @ NgModule.declarations”中列出。 (“         -> [错误->]

如果有人可以帮助我。

1 个答案:

答案 0 :(得分:1)

您需要在in指令中用of替换ngFor来获取值。
基本上,在必须迭代值时使用of,而在必须迭代属性时使用in
请在下面找到详细的代码:

<!-- Using of, which iterates over values -->
<div *ngFor="let star of nb_stars">

<!-- Using in, which iterate over properties -->
<div *ngFor="let star in nb_stars">

根据收到的错误,还必须按如下所示在根模块(app.module.ts)中导入CommonModule:

import { CommonModule } from "@angular/common";
@NgModule({
    imports: [CommonModule]
})
相关问题