1

I like to explore the feasibility with using component Input as Event output, similar to how react does it.

Parent component

parentHandler(message){
   console.log(message)
}

Parent html

<child [myEvent]="parentHanlder"></child>

Child component

@Input myEvent

ngOnInit(){

this.myEvent('hello')

}

I am wondering if there are disadvantage or potential issue with this approach e.g memory/performance

thank you

Fan Cheung
  • 10,745
  • 3
  • 17
  • 39
  • Does this answer your question? [Angular 5 Component input function that takes parameters](https://stackoverflow.com/questions/48012662/angular-5-component-input-function-that-takes-parameters) – mr. pc_coder Jul 04 '20 at 09:28
  • Not quite. I am aware that input and output both works, I want to know if there is potential issue with using @input only to handle event – Fan Cheung Jul 04 '20 at 09:32
  • can you please explain why you need this? – TAHA SULTAN TEMURI Jul 04 '20 at 09:33

2 Answers2

1

This will work perfectly as expected.

The behavior will be similar to @Output except the context of execution.

The scope of execution will be with child component

export class AppComponent {
 version: number = 7;
  messageMe(param){
    alert("Message Me.."+param+this.version); 
  }
}

<framework
[messageHandler]="messageMe"></framework>


export class FrameworkComponent implements OnInit {
  @Input()
  messageHandler: Function;

  name="Jone";

  ngOnInit() {
    this.messageHandler(this.name);
  }
}
blazehub
  • 1,880
  • 19
  • 25
0

You can use setter in this situation

Parent:

<child [myEvent]="parentHanlder"></child>

Child (ts):

@Input()
set myEvent(parentHandler: string) {
     this.myEvent('hello');
}
Majid Nayyeri
  • 447
  • 3
  • 15