简体   繁体   English

Angular 中的错误:无法绑定到“ngForOf”,因为它不是“div”的已知属性

[英]Error in Angular: Can't bind to 'ngForOf' since it isn't a known property of 'div'

I can't use directive *ngFor.我不能使用指令 *ngFor。 I've searched all the posible solutions, but I can't fix it.我已经搜索了所有可能的解决方案,但我无法修复它。

Here is my code:这是我的代码:

sobre-nosotros.component.html: sobre-nosotros.component.html:

<div class="card" style="width: 18rem;" *ngFor="let d of test">
    <img src="assets/img/1.jpg" class="card-img-top" alt="...">
    <div class="card-body">
      <h5 class="card-title">Card title</h5>
      <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
      <a href="#" class="btn btn-primary">Go somewhere</a>
    </div>
  </div>

sobre-nosotros.component.ts sobre-nosotros.component.ts

import { Component, OnInit } from '@angular/core';
import { CochesInterface } from '../interfaces/CochesInterface';
import { CochesServiceService } from '../services/coches-service.service';

@Component({
  selector: 'app-sobre-nosotros',
  templateUrl: './sobre-nosotros.component.html',
  styleUrls: ['./sobre-nosotros.component.css']
})
export class SobreNosotrosComponent implements OnInit {

  public test:number[]=[1,2,3,4,5];

  constructor(private cochesService: CochesServiceService) {
  }

  ngOnInit() {
    
  }
}

And I've following error:我有以下错误:

在此处输入图片说明

My app.module我的 app.module

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

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HeaderComponent } from './components/header/header.component';
import { FooterComponent } from './components/footer/footer.component';

import { InicioComponent } from './inicio/inicio.component';

@NgModule({
  declarations: [
    AppComponent,
    HeaderComponent,
    FooterComponent,
    InicioComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

I don't know what could be the problem.我不知道可能是什么问题。 Is a project recently created, so I don't touched anything.是最近创建的一个项目,所以我没有接触任何东西。

You need to import the CommonModule as ngIf and ngFor are declared there.您需要导入CommonModule作为ngIfngFor在那里声明。

Ref:Frequently-used modules参考:常用模块

You should declare your SobreNosotrosComponent in the app.module , like you did InicioComponent .你应该在SobreNosotrosComponent中声明你的app.module ,就像你做InicioComponent Then stop and relaunch ng serve process.然后停止并重新启动ng serve进程。 This should fix it.这应该解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法绑定到“ngForOf”,因为它不是“div”的已知属性 - Can’t bind to ‘ngForOf’ since it isn’t a known property of ‘div’ 无法绑定到“ngForOf”,因为它不是“div”的已知属性 - Can't bind to 'ngForOf' since it isn't a known property of 'div' 无法绑定到“ngForOf”,因为它不是 angular 5 中的已知属性 - Can't bind to 'ngForOf' since it isn't a known property in angular 5 Angular 无法绑定到“ngForOf”,因为它不是“div”的已知属性 - Angular Can't bind to 'ngForOf' since it isn't a known property of 'div' Angular 11 错误:无法绑定到“ngForOf”,因为它不是“tr”的已知属性 - Angular 11 error : Can't bind to 'ngForOf' since it isn't a known property of 'tr' 无法绑定到“ ngForOf”,因为它不是“ tr”的已知属性 - Can't bind to 'ngForOf' since it isn't a known property of 'tr' 无法绑定到“ngForOf”,因为它不是“li”的已知属性 - Can't bind to 'ngForOf' since it isn't a known property of 'li' 无法绑定到“ngForOf”,因为它不是组件的已知属性 - Can't bind to 'ngForOf' since it isn't a known property of component 无法绑定到“ngForOf”,因为它不是“tbody”的已知属性 - Can't bind to 'ngForOf' since it isn't a known property of 'tbody' 无法绑定到“ngForOf”,因为它不是“li”的已知属性 - Can't bind to 'ngForOf' since it isn't a known property of 'li'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM