角度 4:ng 对于内部 html 不起作用



这是我的产品组件.html

<div class="col-md-6">
<table class="table table-bordered table-responsive">
<thead>
<tr>
<th class="text-center">Име</th>
<th class="text-center">Фамилия</th>
<th class="text-center">Град</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let i as months">
<td>{{i}}</td>
</tr>
</tbody>
</table>
</div>

这是我的产品.component.ts

import { Component } from '@angular/core';
@Component({
selector: 'app-product',
templateUrl: './product.component.html',
styleUrls: ['./product.component.css']
})

导出类产品组件 {

months = ["January", "Feburary", "March", "April", "May", 
"June", "July", "August", "September",
"October", "November", "December"];

}

这是我的应用程序.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import {RouterModule} from'@angular/router';
import { FormsModule } from '@angular/forms';

import { AppComponent } from './app.component';
import { ProductComponent } from './product/product.component';
import { ItemComponent } from './item/item.component';
@NgModule({
declarations: [
AppComponent,
ProductComponent,
ItemComponent
],
imports: [
BrowserModule,
FormsModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

我没有用 ngFor 在 html 中打印我的月份,因为错误"无法绑定到'ngForAs',因为它不是'tr'的已知属性。("">

首先,您的导入中缺少CommonModule。将其添加到您的AppModule中:

import { CommonModule } from `@angular/common';
imports: [
CommonModule, 
BrowserModule,
FormsModule,
],

其次,*ngFor语法是错误的。将其更正为以下内容。

<tr *ngFor="let i of months">

最新更新