簡體   English   中英

無法綁定到“ngForOf”,因為它不是“li”的已知屬性

[英]Can't bind to 'ngForOf' since it isn't a known property of 'li'

我卡住了。 我得到標題中的錯誤。 在 html 中,它顯示 messageBundle object 並且它具有完整數據(因此沒有丟失數據)。 數據是異步獲取的,那部分工作正常,我得到了正確的數據並可以顯示它,但我不能遍歷它。 據我所知,循環的語法是正確的,因為它正在處理幾乎相同的其他組件。 app.module 有 BrowserModule,在子組件上我嘗試添加 CommonModule 和 BrowserModule 沒有區別。 從各方面來看,這應該有效,但事實並非如此。 我如何解決它?

組件.html:

<div class = "contactList">
        <p>Contacts</p>
        <div >
            <div>{{messageBundle.contacts[0]}}</div>
            <li *ngFor="let contact of messageBundle.contacts">
                <span>{{contact.full_name}}</span>
            </li>
        </div>
    </div>

組件.ts:

import {Component, OnInit, OnDestroy} from "@angular/core";
import { Subscription } from 'rxjs';
import { MessagingService } from '../message.service';
import { MessageBundle } from '../message-bundle.model';
import { BrowserModule } from '@angular/platform-browser';//does nothing

@Component({
  selector: 'app-message',
  templateUrl: './message.component.html',
  styleUrls: ['./message.component.css']
})
export class MessageComponent    {
  private messageBundleListener:Subscription;
  public messageBundle : MessageBundle;

  constructor(private messageService : MessagingService){}

  ngOnInit(){
    this.messageBundle  = this.messageService.getMessageBundle();
    this.messageService.fetch_entire_message_bundle_from_backend_server();
    console.log("updated budnle on component on start"+this.messageBundle);
    this.messageBundleListener = this.messageService.getMessageBundleUpdateListenerAsObservable().subscribe(
      (updatedMessageBundle)=>{
        this.messageBundle= updatedMessageBundle;//gets correct data, data is all there
        console.log("updated bundle on message component");
        console.dir(this.messageBundle)
      }
    );
  }
  ngOnDestroy(){
    this.messageBundleListener.unsubscribe();
  }
}

應用程序模塊.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
...
import { MessageComponent } from './messages/message-window/message.component';

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

沒有語法錯誤。 我在這上面花了一個小時,將 ngFor 更改為 NgFor 所以它引發了一個實際錯誤,將其更改回來,現在它正在工作。 (╯°□°)╯︵┻━┻

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM