angular - 如何动态重新加载自定义组件并为其传递新参数?
问题描述
另一个新手 Angular 问题。我有 3 个选项。When an option in the select is selected, I want the value of the option to be passed to the id argument of my custom component and for that component to refresh itself and display the value of the argument passed to it. 目前,当我将参数的值重置为 mycustomcomponent 时,没有任何反应。
app.component.html
<select (change)="onDoSomethingWithChange($event.target.value)">
<option value="1">Value 1</option>
<option value="2">Value 2</option>
<option value="3">Value 3</option>
</select>
<mycustomcomponent #MyCustomComp [id]=0></mycustomcomponent>
app.component.ts
export class AppComponent implements OnInit {
@ViewChild('MyCustomComp', {static: false}) myCustomComp: MyCustomComponent;
public onDoSomethingWithChange(id : number):void
{
this.myCustomComp.id = id
}
}
mycustomcomponent.component.ts(该组件将简单地获取传递给它的 id 值并显示它。)
export class MyCustomComponent implements OnInit {
@Input() id: number;
}
mycustomcomponent.component.html
You passed an id of value {{id}}
解决方案
您不需要使用“ViewChild”来将选定的值传递给子组件。您可以在父组件中有一个属性来保存“选定值”,并且在每次更新选定值时,您都会更新该值。此属性可以作为输入传递给您的子组件。
推荐阅读
- memory-leaks - 如何理解 WmiPrvSE 进程中明显的内存泄漏?
- asp.net - 从桌面应用到 MVC 的遗传算法
- mysql - 如何设计用于多语言测验的数据库
- android - 圆形可绘制在某些设备上不呈现颜色
- loops - 使用循环按日期移动文件的批处理文件
- c# - Visual Studio 2015 - 命名空间 System.Runtime.Remoting.Channels.Http 中不存在 Http;
- swift - 当被呈现的视图“拥有”时,Swift 无主的自我泄漏
- javascript - 具有固定长度、前导空格和数字的 Javascript 正则表达式模式
- postgresql - 将 pg_restore 与远程文件一起使用
- kubernetes - VPC 网络中链接的不同集群之间的 GCP 中的 Pod 和服务之间的通信