首页 文章

Angular:* ng不显示组件列表

提问于
浏览
1

我是Angular / MEAN的新手 . 我正在按照教程,我没有问题 . 但现在我正在制作我自己的应用程序,它无法正确呈现 - 我无法弄清楚问题是什么 .

所以我有一个模型组件'ENTRY' . 我正在尝试在我的应用程序中显示2个测试条目(在AppComponent构造函数中创建放入数组中),但它们不会显示 .

// 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>

有谁知道我做错了什么?我非常感谢任何帮助 .

1 回答

  • 0

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

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

    您还需要将_条目更改为条目或在app-component中相反(例如user @ user184994指出)

    编辑:添加ngIf,它的工作原理

相关问题