Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get change event in input field for a directive in angular 4

I am a bit new to angular 4. I am trying to get change event from an input field inside a directive. Currently i am working with @HostListener

@HostListener('keyup', ['$event'])
  inputChanged(event) {}

This is working correctly but this event is fired after some delay from releasing key and user can enter wrong input and is able to see that too. In my implementation i have removed the invalid input but it doesn't give a good exposure to user. Only thing i want is to get change event right the moment change happen in input field (character / string enter or remove both ). Current HTML looks like this

<input type='text' class="form-control" placeHolder='hh:mm:ss' time-input [(ngModel)]="params.time" name="time"/>

PS. time-input is the name of directive and in directive i am trying to get the event change and i don't want to move any implementation to controller or component.

like image 406
Ali Khan Avatar asked Jul 19 '17 06:07

Ali Khan


People also ask

What is ng-change directive in AngularJS?

The ng-change directive tells AngularJS what to do when the value of an HTML element changes. The ng-change directive requires a ng-model directive to be present. The ng-change directive from AngularJS will not override the element's original onchange event, both the ng-change expression and the original onchange event will be executed.

How to call function when input control value changed in AngularJS?

Following is the example of using ng-change event with input text control to call function when input control value changed in angularjs applications. If you observe above example we added ng-change event to input textbox control and it will raise event whenever we change the value of input text control in angularjs application.

What is the change event in angular form?

The (change) is a DOM event fires when changes to the form fields like <input> , <select>, and <textarea> is committed by the user. On <select> it fires when the user selects a new option either by a mouse click or using a keyboard. The following example shows how to use the change event in Angular.

What is ng-change event in JavaScript?

The ng-change event is only triggered if there is a actual change in the input value, and not if the change was made from a JavaScript. Supported by <input>, <select>, and <textarea>.


1 Answers

Use

@HostListener('ngModelChange', ['$event'])
like image 192
Günter Zöchbauer Avatar answered Nov 15 '22 08:11

Günter Zöchbauer