当前位置: 首页 > 知识库问答 >
问题:

无法绑定到“ngForOf”,因为它不是“ng container”的已知属性

孟成化
2023-03-14

我正在尝试在表中显示来自数据库的一些数据,我遇到了这个错误,我在网上找到的唯一解决方案是导入通用模块和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;
    });
  }

}
  

管理组成部分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>

共有2个答案

吕英才
2023-03-14

Angular不会在HTML中绑定这个关键字,而是在没有这个的情况下使用它。HTML中的关键字。所以根据你的代码,你只需要改变

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

致:

<ng-container *ngFor="let inv of inventar">
农弘毅
2023-03-14

存在以下问题。

>

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

 类似资料:
  • 我的html模态组件中有以下代码: 但是我得到了以下错误: 我在网上看过其他人的解决方案,他们建议导入表单模块、反应式表单模块和通用模块,但我已经在模式模块中导入了这些解决方案 有没有人对我如何摆脱这个错误还有其他建议?

  • 当试图用电话号码填充简单的无线电组列表时 无法绑定到“ngForOf”,因为它不是“离子项”的已知属性。 问题与角9有关。可能的解决方案是,BrowserMoules和Common模块需要到位。我想我明白了。然而,错误仍然存在。 爱奥尼亚信息: 加上数字。页ts add-number.module.ts 加上数字。页html 你知道怎么解决这个问题吗? . 编辑:https://pastebin.

  • 最近开始玩《棱角2》了。到目前为止都很棒。因此,为了学习使用我开始了一个个人演示项目。 有了基本的路由设置后,我现在想从报头导航到一些路由,但由于我的报头是的父路由,所以我收到这个错误。 app.component.html header.component.html 现在我部分地理解了,我猜由于该组件是的包装器,所以不可能通过这种方式访问。那么,对于这样的场景,是否有可能从外部访问导航呢? 如果

  • 问题内容: 即使未显示组件,启动我的Angular应用程序时也会出现以下错误。 我必须将注释掉,这样我的应用才能正常工作。 我正在查看Hero插件,但与我的代码没有任何区别。 这是组件文件: 问题答案: 是的,就是这样,在app.module.ts中,我刚刚添加了:

  • 我不熟悉角度单元测试。。我正在尝试测试一个模态组件,但不断出现以下错误:“无法绑定到'formGroup',因为它不是'form'的已知属性。” 我尝试过将FormGroup、FormBuilder、Validator传递到测试提供程序中(因为它们是我正在测试的代码中使用的,所以我一次一个地将它们添加到一起),我还尝试将ReactiveFormsMoules添加到它中(与其他提供程序一起,而不与其

  • 我在实现折叠特性时得到了这个错误: 错误:模板解析错误:无法绑定到'target',因为它不是'div'的已知属性 app.component.html: 但是如果我简单地使用,就可以很好地工作。但是当我绑定时,它会出现错误。