Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

Angular 4:ngFor inside html not working

This is in my product.component.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>

This is in my product.component.ts

  import { Component } from '@angular/core';

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

export class ProductComponent {

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

}

This is in my app.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 { }

I is not print my months in html with ngFor, because error "Can't bind to 'ngForAs' since it isn't a known property of 'tr'. (""

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Firstly, you are missing CommonModule in your imports. Add that in your AppModule:

import { CommonModule } from `@angular/common';

imports: [
  CommonModule, 
  BrowserModule,
  FormsModule,
],

Secondly, the *ngFor syntax is wrong. Correct it to following.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...