javascript - 知道这个角度主细节实现可能缺少什么吗?
问题描述
Master.component.html 在浏览器加载时显示 2 个任务:
<div class="parent">
<ol>
<li
*ngFor="let task of tasks"
(click)="onSelect(task)"
[class.selected]="task === selectedTask"
>
{{task.text}} - {{task.done}}
</li>
</ol>
<app-detail [task]="selectedTask"></app-detail>
</div>
When a task in the list above is selected, the onSelect(task) method in the Master.component.ts gets triggered. onSelect(task) 方法当前为空。我希望所选任务显示在 Detail.component.html 中,但目前仅“加载详细信息!” 被展示:
<div *ngIf="task" class="detail">
<h3>you selected {{task.text}}</h3>
<table>
<tr>
<td>Text:</td>
<td>{{ task.text }}</td>
</tr>
<tr>
<td>Status:</td>
<td>{{ task.done }}</td>
</tr>
</table>
</div>
{{ task }}
detail loads!
这是 Detail.component.ts:
import { Component, OnInit, Input } from '@angular/core';
import { Task } from '../models/task';
@Component({
selector: 'app-detail',
templateUrl: './detail.component.html',
styleUrls: ['./detail.component.css']
})
export class DetailComponent implements OnInit {
@Input() task : Task
constructor() { }
ngOnInit() {
}
}
我创建了这个实现作为我在 youtube 上找到的这个示例的精简版本:https ://www.youtube.com/watch?v=3InW7kyPDCw
知道为什么 Master 中的选定任务没有详细显示吗?
解决方案
您只需要在选择器上添加*ngIf以便在selectedTask具有值app-detail
时加载它,
这是一个例子
<div class="parent">
<ol>
<li
*ngFor="let task of tasks"
(click)="onSelect(task)"
[class.selected]="task === selectedTask"
>
{{task.text}} - {{task.done}}
</li>
</ol>
<app-detail *ngIf="selectedTask" [task]="selectedTask"></app-detail>
</div>
这是Stackblitz演示
推荐阅读
- arrays - 将布尔数组作为函数输入传递时初始化器值过多
- python - 有没有办法在划分列表后只接受整数值?已回答
- python - 试图计算文本文件的每一行中有多少个数字
- php - cant parse DateTime::__construct()
- html - Rmarkdown“保存”html页面的确切位置
- excel - 在 Excel 2019 中使用 VBA 生成带有国家符号的文本时出现问题
- android - C++ 防止链接器丢弃函数
- arrays - Jmeter - ArrayList 中的 JSON 响应操作附加节点
- python-3.x - 使用 wfdb 进行心电图信号 QRS 检测
- php - 将产品自定义字段保存为 WooCommerce 管理员手动订单的自定义订单项元数据