Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 and jQuery 'change' events

I have a hidden input to control the changes made by jQuery, which includes an event of angular2 change, like this

<input id='input1' hidden (change)='onChange($event)'>

Then in my case, I have to use jQuery to change value of this input, then trigger change event:

$('#input1').val('somevalue').trigger('change');

This change event that I triggered via jQuery only works with jQuery, but not Angular2:

//This is working
$('#input').change(function(){
  console.log('Change made');
})

In angular2 component:

//This is not working
onChange(): void{
  console.log('Change made');
}

How can I work around in this situation?

like image 697
Pho Huynh Avatar asked Jul 24 '16 21:07

Pho Huynh


People also ask

Can I use jQuery and Angular together?

Yes, Angular can use jQuery if it's present in your app when the application is being bootstrapped. If jQuery is not present in your script path, Angular falls back to its own implementation of the subset of jQuery that we call jQLite.

What is the use of change event in Angular?

NgModelChange Vs Change Change is a DOM Event and has nothing to do with the Angular. You can use change event on <input>, <select>, and <textarea> form elements. Change event fires when you remove the focus from input text after changing the content.

What is the difference between change and onChange?

The difference is that the oninput event occurs immediately after the value of an element has changed, while onchange occurs when the element loses focus, after the content has been changed.


2 Answers

You could assign a template reference variable to the <input>, like #hiddenInput1, get a hold of its native element (via @ViewChild) inside the component class and then use jQuery itself to listen to the change event.

Demo plunker here.

import { Component, ViewChild, ElementRef, AfterViewInit } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `
  <h1>My First Angular 2 App</h1>
  <hr>
  <input id='input1' hidden #hiddenInput1>
  `
})
export class AppComponent implements AfterViewInit  {

  @ViewChild('hiddenInput1') hiddenInput1:ElementRef;

  ngAfterViewInit() {
    $(this.hiddenInput1.nativeElement).on('change', (e) => {
      console.log('Change made -- ngAfterViewInit');
      this.onChange(e);
    });
  }

  onChange(): void{
    console.log('Change made -- onChange');
  }

}
like image 191
acdcjunior Avatar answered Sep 17 '22 18:09

acdcjunior


Angular2 change event is added via native addEventListener.

You cannot trigger a native event with jQuery's .trigger('change'). So you will need to create a native event and dispatch it:

 const customEvent = document.createEvent('Event');  
 customEvent.initEvent('change', true, true);
 $('#input1')[0].dispatchEvent(customEvent);

This way angular2 will fire onChange handler.

Here is a demo plunker

As @Cristal Embalagens mentioned in comments you need to use input event for angular2 because DefaultValueAccessor is subscribing on this event:

@Directive({
  selector:
      'input:not([type=checkbox])[formControlName],textarea[formControlName],input:not([type=checkbox])[formControl],textarea[formControl],input:not([type=checkbox])[ngModel],textarea[ngModel],[ngDefaultControl]',
  host: {'(input)': 'onChange($event.target.value)', '(blur)': 'onTouched()'},
  providers: [DEFAULT_VALUE_ACCESSOR]
})
export class DefaultValueAccessor implements ControlValueAccessor {

Some Example

like image 40
yurzui Avatar answered Sep 20 '22 18:09

yurzui