angular 4:由于它不是'li'的已知属性,因此无法绑定到'ngForFor'

sa_1130

我刚刚开始学习angular4。这是我尝试在Visual Studio Code中实现的简单代码,但仍然会出现此错误。

Uncaught Error: Template parse errors:
Can't bind to 'ngForFor' since it isn't a known property of 'li'. ("
</ul>
<ul>
 <li [ERROR ->]*ngFor="let hobby for hobbies">{{hobby}}</li>
</ul>"): ng:///AppModule/UserComponent.html@6:6
Property binding ngForFor not used by any directive on an embedded template. 
Make sure that the property name is spelled correctly and all directives are 
listed in the "@NgModule.declarations".("
</ul>
<ul>
[ERROR ->]<li *ngFor="let hobby for hobbies">{{hobby}}</li>
</ul>"): ng:///AppModule/UserComponent.html@6:2

我尝试了将CommonModule添加到app.module文件的先前解决方案。但这并没有解决问题。我无法弄清楚出了什么问题。

app.component.ts:

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

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

}

app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';


import { AppComponent } from './app.component';
import { UserComponent } from './components/user/user.component';
import {CommonModule} from '@angular/common';

@NgModule({
  declarations: [
    AppComponent,
    UserComponent
  ],
  imports: [
    BrowserModule,CommonModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

user.component.ts:

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


@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.css']
})
export class UserComponent implements OnInit {
  name:string;
  age:number;
  address: Address;
  hobbies:string[];


  constructor() { 
    console.log('constructor ran ...');
  }

  ngOnInit() {
  console.log('ngOnInit ran ...');
  this.name='Raul';
  this.age=22;
  this.address= {
    street:'abc',
    city:'xyz',
    country: 'jkl'
  }
  this.hobbies=['reading','playing','swimming'];
  }

}

interface Address{
  street:string,
  city:string,
  country:string

}

user.component.html:

<h1>{{name}}</h1>
<ul>
  <li>Age:{{age}}</li>
  <li>Address:{{address.street}}, {{address.city}},{{address.country}}</li>
</ul>
<ul>
  <li *ngFor="let hobby for hobbies">{{hobby}}</li>
</ul>
萨拉(Sachila Ranawaka)

应该of而不是for里面ngFor

ngFor="let hobby of hobbies"

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Angular 5 错误:无法绑定到“ngForFor”,因为它不是“li”的已知属性

无法绑定到“ ngForFor”,因为它不是“ li”的已知属性

控制台Angular 9中的警告:由于它不是'li'的已知属性,因此无法绑定到'ngForOf'

Angular4异常:由于它不是'input'的已知属性,因此无法绑定到'ngClass'

无法绑定到“ngForFor”,因为它不是“<tr>”的已知属性

Angular 2:由于它不是'input'的已知属性,因此无法绑定到'ngModel'

Angular 2+和Observables:由于它不是'select'的已知属性,因此无法绑定到'ngModel'

Angular 2:由于它不是已知的本机属性,因此无法绑定到x

angular2测试:由于它不是'input'的已知属性,因此无法绑定到'ngModel'

Angular2异常:由于它不是已知的本机属性,因此无法绑定到“ routerLink”

Angular7-由于它不是'mat-table'的已知属性,因此无法绑定到'dataSource'

无法绑定到“ ngForOf”,因为它不是“ li” Angular 9的已知属性

无法绑定到“ ngForOf”,因为它不是ngFor Angular中“ li”的已知属性

angular 5-ng2-file-upload:由于它不是'div'的已知属性,因此无法绑定到'uploader'

模板解析错误:由于它不是Angular 2中的已知本机属性,因此无法绑定到“ ng-class”

Angular2快速入门教程突破业力测试-“由于它不是'input'的已知属性,因此无法绑定到'ngModel'。

Angular 无法绑定到“dirUnless”,因为它不是已知属性

Angular 4无法绑定到<property>,因为它不是<component>的已知属性

Angular 4无法绑定到“ formGroup”,因为它不是“ form”的已知属性

Angular 4“无法绑定到'ngModel',因为它不是'input'的已知属性。”

自定义指令:由于它不是element的已知属性,因此无法绑定到指令

角度6:失败:模板解析错误:由于它不是'a'的已知属性,因此无法绑定到'routerLink'。(

尽管导入了FormsModule,但由于它不是'input'的已知属性,因此无法绑定到'ngModel'

角度灯箱:由于它不是“ a”的已知属性,因此无法绑定到“数据灯箱”

角2:模板解析错误:由于它不是'input'的已知属性,因此无法绑定到'ngModel'

模板解析错误:由于它不是'a'的已知属性,因此无法绑定到'routerLink'

AngularJS 2:由于它不是'input'的已知属性,因此无法绑定到'ngModel'

模板解析错误:由于它不是'div'的已知属性,因此无法绑定到DIRECTIVE

模板解析错误:由于它不是'input'的已知属性,因此无法绑定到'ngbTypeahead'