首页 > 解决方案 > Angular:*ngFor 不显示组件列表

问题描述

我是 Angular/MEAN 的新手。我正在按照教程进行操作,那里没有问题。但是现在我正在制作自己的应用程序,但它无法正确呈现 - 我无法弄清楚问题所在。

所以我有一个模型+组件'ENTRY'。我试图在我的应用程序中显示 2 个测试条目(在 AppComponent-constructor 中创建 + 放入数组中),但它们没有显示。

// entry.model.ts

import { Segment } from '../segment/segment.model';
import { Marker } from '../marker/marker.model';

export class Entry {
    private _id: string;
    private _title: string;
    private _description: string;
    private _dateCreated: Date = new Date();
    private _dateLastModified;
    private _contents : string;

    constructor(title : string, description : string, contents : string){
        this._title = title;
        this._description = description;
        this._contents = contents;
    }

    public get title() : string {
        return this._title;
    }

    get description() : string {
        return this._description;
    }

    get contents() : string {
        return this._contents;
    }

}

// entry.component.ts

import { Component, OnInit, Input } from '@angular/core';
import { Entry } from './entry.model';

@Component({
  selector: 'app-entry',
  templateUrl: './entry.component.html',
  styleUrls: ['./entry.component.css']
})
export class EntryComponent implements OnInit {
  @Input() public entry: Entry;

  constructor() { }

  ngOnInit() {
  }

}

// entry.component.html

<p>
  This entry is called {{title}}.
  Its description is {{description}}.
  its contents are {{contents}}.
</p>

// app.component.ts

import { Component } from '@angular/core';
import { Entry } from './entry/entry.model';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  private _entries = new Array<Entry>();

  constructor() {
    const entry1 = new Entry("My Title",
    "This is an overly long description intended as an example, capiche?",
    "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.");
    this._entries.push(entry1);
    const entry2 = new Entry("My second title",
    "Some other description for testing purposes.",
    "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.");
    this._entries.push(entry2);
  }
}

// app.component.html

<!-- This one works, in that the html gets rendered but without any properties -->
<app-entry></app-entry>

<!-- All these other ones don't do anything -->
<div *ngFor='let currEntry of entries'>
  <app-entry></app-entry>
</div>
<app-entry *ngFor='let entry of entries'>
  {{entry}}
</app-entry>

<app-entry *ngFor='let currEntry of entries'
[entry]='currEntry'></app-entry>

<div>
  <ul>
    <li *ngFor='let currEntry of entries'>
      <app-entry [entry]='currEntry'></app-entry>
    </li>
  </ul>
</div>

有谁知道我做错了什么?我将不胜感激任何帮助。

标签: angularngfor

解决方案


我想为了从视图中访问条目输入字段(道具),你应该这样做

<p *ngIf="entry" >
  This entry is called {{entry.title}}.
  Its description is {{entry.description}}.
  its contents are {{entry.contents}}.
</p>

您还需要在应用程序组件中将 _entries 更改为 entry 或其他方式(如用户@user184994 指出的)

编辑:添加了 ngIf 并且它有效


推荐阅读