【发布时间】:2021-03-31 12:30:41
【问题描述】:
我正在尝试在 Angular 中做一个简单的@input 绑定过程。我做了大部分。我不知道为什么,但我无法获取数据。我认为我的 .map 使用有问题。
这是我的父组件.ts
export class DashboardContainerComponent implements OnInit { cards: { title: string, body: string }[];
constructor(private http: HttpClient) { }
ngOnInit(): void {
this.http.get("https://jsonplaceholder.typicode.com/comments").subscribe((comments: any) => {
cts
// this.cards = comments. ... map? reduce? filter?
this.cards = comments.map(n => {
return {title: n["name"], body: n["body"] };
} )
}) }
}
这是子组件.ts
export class MyCardComponent implements OnInit {
@Input() item: {title: string, body: string}[];
// TODO: define @Input(s) here
constructor() { }
ngOnInit(): void {
}
}
这就是我在父组件中使用此绑定的方式。html
<div class="dashboard-container">
<h1>Comments</h1>
<ng-container *ngIf="!cards">
<div class="info-text">Cards will appear here.</div>
</ng-container>
<ng-container *ngFor="let card of cards">
<!-- TODO: assign input(s) below in app-my-card -->
<app-my-card [item]="cards"></app-my-card>
</ng-container>
</div>
这就是我在子 component.html 中使用此绑定的方式
<div class="card-container">
<div class="card-title">
<h1>{{item.title}}</h1>
</div>
<div class="card-body">
<p>{{item.body}}</p>
</div>
</div>
【问题讨论】:
标签: angular function binding components