1

I know you can do something like

<input (input)="doSomething($event)" />
<input (input)="boolVar = $event.target.value > 5" />

but how can I do something like

funcVar = (e) => {
  console.log(e.target.value)
}

<input (input)="funcVar" />

I tried other things like

<input [input]="funcVar" />
<input [(input)]="funcVar" />

but no luck. Reason I'm doing this is that our forms are being generated from a data set, so they only way I can add on stuff like is by passing in stuff through variables that will generate the forms.

1 Answer 1

2

The event handler should call the function. The markup would be:

<input (input)="funcVar($event)" />

for the funcVar member defined as:

public funcVar = (e) => {
  console.log(e.target.value)
}

If another method is assigned to funcVar later, that new method will be executed when the input event is triggered:

someMethodExecutedLater() {
  this.funcVar = (e) => {
    // From now on, this new function will be called by the event handler
    ...
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

So is there no way to pass in funcVar($event) through a variable? Since I can't hard code that onto the input. I need to do something like <input (input)="funcVar"> where funcVar is holding the function. Or a way to hard code it like that, but override what function funcVar is on the fly. Need it to try and evaluate what I put on the right hand side (as it will be a variable holding a function) and then call it.
Because funcVar is a variable that will change what function it's holding based on the form that's being generated
Oh wait, I think I see what you are saying (one minute)
Makes perfect sense. I was overthinking things. Thinking it wouldn't look up funcVar (it's late in the day) :D Thanks for the explanation!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.