Angular pass callback function to child component as @Input similar to AngularJS way

后端 未结 10 1603
庸人自扰
庸人自扰 2020-11-27 10:22

AngularJS has the & parameters where you could pass a callback to a directive (e.g AngularJS way of callbacks. Is it possible to pass a callback as an @Input

10条回答
  •  一整个雨季
    2020-11-27 10:34

    An alternative to the answer Max Fahl gave.

    You can define callback function as an arrow function in the parent component so that you won't need to bind that.

    @Component({
      ...
      // unlike this, template: '',
      template: '',
      directives: [ChildComponent]
    })
    export class ParentComponent {
    
       // unlike this, public theCallback(){
       public theCallback = () => {
        ...
      }
    }
    
    @Component({...})
    export class ChildComponent{
      //This will be bound to the ParentComponent.theCallback
      @Input()
      public myCallback: Function; 
      ...
    }

提交回复
热议问题