3

I have an EventEmitter "action". How can I set the initial Value? For example it's always 'methodX' until I change it to 'methodY'..

Something like this:

export class FieldComponent {
  @Output() action: EventEmitter<any> = methodX;

  constructor() {
  }

  public methodX() {
  }
}
guwluws
  • 211
  • 1
  • 3
  • 13
  • Take a look at this: https://stackoverflow.com/questions/52598734/angular-six-set-initial-value-for-output-eventemitter – Nostix May 03 '19 at 14:02
  • why you need that where action need pass from parent component? – Hien Nguyen May 03 '19 at 14:02
  • 1
    I could not see any reason to assign a method and change in future, might be you will have some special case, could you please share the scenario? OR you mis-understood the EventEmitter... – Ali Adravi May 03 '19 at 14:08
  • @Nostix that's just an initial value **for the** EventEmitter-**Action**. I want an initial EventEmitter-**Action**... – guwluws May 03 '19 at 14:09
  • What do you mean by "EventEmitter action"? – ConnorsFan May 03 '19 at 14:11
  • @AliAdravi well I have an Input-Field which focuses the next Input-Field. In the future I want to change the direction of the focusing (previous element), or await for something, ... Isn't it the right way? – guwluws May 03 '19 at 14:11
  • @ConnorsFan in the linked question he tried to hand over an parameter to the action. But that's not my problem.. – guwluws May 03 '19 at 14:14
  • For just to change the order of tab you can simply use html attribute `tabindex`, event emitter are mostly used to get data from child to parent! – Ali Adravi May 03 '19 at 14:15
  • @AliAdravi it's not just to change the order. Another example is when finishing editing, I want to create a child element and after creation focus it. I'm pretty sure dealing with EventEmitter is the best way... – guwluws May 03 '19 at 14:24

2 Answers2

0

test this:

@Output() action: EventEmitter<any> = new EventEmitter<any>();


ngOnInit() {
 this.action.emit({{initial value}});
 }

public methodX() {
console.log(action);
  }
Suliman Farzat
  • 1,197
  • 11
  • 12
0

I'm not sure if you can do this like you are wanting to. EventEmitter is just an abstraction of rxjs Subjects. If you use Subjects, you can use the BehaviorSubject and have an initial value, but the default Subject cannot.

XXIV
  • 348
  • 1
  • 5
  • 24