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

后端 未结 10 1602
庸人自扰
庸人自扰 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:43

    An alternative to the answer SnareChops gave.

    You can use .bind(this) in your template to have the same effect. It may not be as clean but it saves a couple of lines. I'm currently on angular 2.4.0

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

提交回复
热议问题