【问题标题】:Can't bind to 'ngForOf' since it isn't a known property of 'ng-container'无法绑定到“ngForOf”,因为它不是“ng-container”的已知属性
【发布时间】:2021-03-20 14:32:03
【问题描述】:

我正在尝试在表格中显示数据库中的一些数据,我收到此错误,我在网上找到的唯一解决方案是导入 CommonModule 和 NgModule,我已经在这样做了,所以我不明白出了什么问题。 这只发生在这个组件中,不会发生在同一模块中的其他组件中:

Can't bind to 'ngForOf' since it isn't a known property of 'ng-container'.

我收到的数据:

{"body":[{"id":"2","uid":"100002","name":"Telt","created":"2020-12-06 16:53:08","location":"Huset","status":"","weight":"10","size":"NULL","content":"","image":"","comment":"Test"},{"id":"3","uid":"100003","name":"Kano","created":"2020-12-06 17:28:48","location":null,"status":"{\"status\":\"I\",\"user\":\"root\",\"timeStamp\":\"NULL\"}","weight":null,"size":"{\"dimentions\":{\"x\":\"0\", \"y\":\"0\", \"z\":\"0\", \"r\":\"0\"}, \"volume\":\"0\", \"properties\":{\"cube\":\"true\", \"ball\":\"false\", \"cylinder\":\"false\"}}","content":"{\"content\":[{\"invId\":\"NULL\", \"number\":\"0\"}], \"totalWeight\":\"0\"}","image":"\/assets\/images\/default.png","comment":null},{"id":"4","uid":"100004","name":"idk","created":"2020-12-06 17:54:58","location":null,"status":"{\"status\":\"I\",\"user\":\"root\",\"timeStamp\":\"NULL\"}","weight":null,"size":"{\"dimentions\":{\"x\":\"0\", \"y\":\"0\", \"z\":\"0\", \"r\":\"0\"}, \"volume\":\"0\", \"properties\":{\"cube\":\"true\", \"ball\":\"false\", \"cylinder\":\"false\"}}","content":"{\"content\":[{\"invId\":\"NULL\", \"number\":\"0\"}], \"totalWeight\":\"0\"}","image":"\/assets\/images\/default.png","comment":null}],"itemCount":3}

admin.component.ts:

import { InventarService } from './../inventar.service';
import { Inventar } from './../inventar';
import { Component, OnInit } from '@angular/core';




@Component({
  selector: 'app-admin',
  templateUrl: './admin.component.html',
  styleUrls: ['./admin.component.scss'],
})
export class AdminComponent implements OnInit {
  inventar: Inventar[];
  error = '';
  success = '';

  constructor(private inventarService: InventarService) { }

  ngOnInit() {
    this.getInventar();
  }

  getInventar(): void {
    this.inventarService.getAll().subscribe((res: Inventar[]) => {
      this.inventar = res['body'];
    },
    (err) => {
      this.error = err;
    });
  }

}
  

admin.component.html:

  <ion-header [translucent]="true">
      <app-navbar></app-navbar>
    </ion-header>
    
    
    <ion-content>
      <ion-grid fixed>
        <ion-row  class="ion-text-center ion-padding-top">
          <ion-col size="12">
            <ion-title>Admin</ion-title>
          </ion-col>
        </ion-row>
      </ion-grid>
    
      <div *ngIf="error">{{error}}</div>
    <div *ngIf="success">{{success}}</div>
        
    <div id="theList">
      <table>
        <thead>
          <tr>
            <th>Navn</th>
            <th>UID</th>
            <th>Status</th>
          </tr>
        </thead>
        <tbody>
          <ng-container *ngFor="let inv of this.inventar">
            <tr>
              <td> {{ inv['name'] }} </td>
              <td> {{ inv['uid'] }} </td>
              <td> {{ inv['status'] }} </td>
            </tr>
          </ng-container>
          
        </tbody>
      </table>
    
    
    
      
    </div>
      
    </ion-content>

【问题讨论】:

  • 这能回答你的问题吗:stackoverflow.com/questions/40331549/… ?
  • @griFlo 不是真的,我已经确定我在根模块和 Home 模块中都导入了 CommonModule 和 BrowserModule 这个组件
  • 你能提供一个stackblitz吗?因为我仍然怀疑缺少模块导入。
  • @AakashGarg 这里stackblitz.com/edit/ionic-5aoyp2我以前从来没有做过,所以我不确定它是否有效,我只是从我的 github repo 中提取了整个东西
  • @Martin 在你的 stackblitz 中没有使用 ngFor。

标签: angular


【解决方案1】:

Angular 不会在 HTML 中绑定 this 关键字,而是在 HTML 中不使用 this. 关键字来使用它。所以根据你的代码,你只需要改变

<ng-container *ngFor="let inv of this.inventar">

收件人:

<ng-container *ngFor="let inv of inventar">

【讨论】:

  • 啊,我已经在我的代码中更改了它,但我仍然得到同样的错误
【解决方案2】:

存在以下问题。

  1. BrowserModule 只能导入到您的 AppModule 中。从 homemodule 中删除它。

  2. 将您的 AdminComponent 和 NavbarComponent 从 HomeModule 移动到 AppModule。

【讨论】:

  • 谢谢!这解决了我遇到的问题
猜你喜欢
  • 2021-10-24
  • 2021-06-16
  • 1970-01-01
  • 2021-01-06
  • 1970-01-01
  • 1970-01-01
  • 2019-10-09
  • 2020-06-17
  • 2016-06-02
相关资源
最近更新 更多