【问题标题】:Angular 2 pass event from one component to the nextAngular 2将事件从一个组件传递到下一个组件
【发布时间】:2016-11-18 02:46:46
【问题描述】:

父组件我有一个客户列表,我正在传递客户 ID。

<a (click)="onClient($event, ID)">edit</a> View

export class ListComponents
 public myformdata: number;
  onClient(ID: number) {

       console.log('loading ID', JSON.stringify(ID));

       this.myformdata = (ID);
       return this.myformdata;
    }
}`

在子构造函数上我可以绑定它。但是每次单击子构造函数时如何注入该值。我尝试了一个生命周期钩子

ngOnChanges(): void {

}
export class ChldComponents{
    constructor(public _dashlit: ListComponents) {
}
}

【问题讨论】:

    标签: angular inject


    【解决方案1】:

    我不能 100% 确定您要完成什么。如果您只是尝试将 myformdata 传递给 childComponent,则可以在单击父组件时对其进行设置

    parent.component.ts

    onClient(ID: number){
        this.myformdata = ID;
    }
    

    然后使用输入指令将其传递给您的子组件

    child.component.html

    <child-component [myInput]="myformdata"></child-component>
    

    现在它将在您的子组件中可用,并且会在父组件更改 this.myformdata 时更改。如果您需要在输入更改时运行某些功能,您可以使用 OnChanges 生命周期挂钩。

    https://angular.io/docs/ts/latest/cookbook/component-communication.html https://angular.io/docs/ts/latest/api/core/index/OnChanges-class.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-29
      • 2019-03-11
      • 1970-01-01
      • 1970-01-01
      • 2016-03-09
      • 1970-01-01
      • 2019-07-04
      • 2021-10-31
      相关资源
      最近更新 更多