angular - 当我使用 Angular 单击以下项目结构的编辑按钮时,如何填充反应式表单?
问题描述
我正在使用 Angular 和响应式表单创建一个简单的待办事项应用程序,当在以下项目结构中单击“编辑”按钮时,我无法弄清楚如何填充表单。
项目结构:
- 应用程序
- 共享
- 任务详细信息.model.ts
- 任务详细信息.service.ts
- 任务详情
- 任务详细信息表单
- task-details-form.component.html -> 包含响应式表单
- 任务详细信息-form.component.ts
- 任务详细信息.component.html
- 任务详细信息.component.ts
- 任务详细信息表单
- 共享
所以 task-details.component.html 包含所有待办事项的列表,每个待办事项都有“编辑”和“删除”按钮。
这是我的 task-details.component.html
<app-task-detail-form></app-task-detail-form>
<ul>
<li *ngFor="let t of service.listOfItems">
{{t.title}} = {{t.isCompleted}}
<button (click)="onDelete(t.taskId)">Delete</button>
<button (click)="populate(t)">Edit</button>
</li>
</ul>
我的问题是:
当我单击 task-details.component.ts 中的 populate(t) 时,如何在 task-details-form.component.html 中填充反应式表单。我知道我必须将“t”从 task-details.component.html 发送到 task-details-form.component.html,但是当我有上述项目结构时,我该怎么做呢?
解决方案
您可以将待办事项<app-task-detail-form>
作为输入传递给
任务详细信息.component.html
<app-task-detail-form [todo]="selectedTodo"></app-task-detail-form>
任务详细信息-form.component.ts
export class TaskDetailFormComponent {
@Input() todo: Todo;
任务详细信息.component.ts
selectedTodo: Todo;
populate(todo:Todo){
this.selectedTodo = todo;
//... the rest of your code in populate
}
最后的话
项目结构与这里的任何解决方案都无关。只要您的组件在 app.module.ts 中声明,它们就可以在任何地方。
推荐阅读
- javascript - 如何在 Google AppMaker 中使用脚本更改小部件的数据源?
- javascript - 我正在尝试在 ar.js 上做一个项目
- javascript - jQuery:用点击时的输入值替换正文文本出现
- sql - BigQuery 标准 SQL 中的“yearweek”函数的等价物是什么?
- python - (Python) 使用文件输入和输出按销售额对电影进行排序
- android - Django 应用程序的 PWA 清单应该位于哪里?
- gdb - 使用 openocd 和 gdb 在 STM32L4 芯片上通过半主机获取额外字节
- html - 将 html 内容保存为 docx 格式,该格式不在 ms word 中打开
- php - 在电子邮件通知上显示 WooCommerce 自定义结帐字段值
- mysql - MySQL 错误 1215 - 外键(日期类型)到主键(日期类型)