You need to enable JavaScript to run this app. Angular中如何动态重载自定义组件并传入新参数
优惠活动
大模型
产品
解决方案
定价
更多

Angular中如何动态重载自定义组件并传入新参数

Angular下拉传参后自定义组件无响应问题

这是Angular新手开发场景下的典型问题:开发者搭建了包含3个选项的下拉选择框,需求为选中下拉框某一选项时,将选中项的值传递给自定义组件的id入参,使该自定义组件自动刷新并展示接收到的参数值;目前实际效果为,对自定义组件的传入参数重新赋值后,组件没有任何响应。


各文件代码实现

  • 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}}

内容的提问来源于stack exchange,提问作者Fred2020

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.19 02:45:01