angular - ANGULAR2: How to receive an external elementRef inside a custom directive -


i use template:

<input mydirective [other]='field2'> <input #field2> 

and directive:

import { directive, input, output, elementref, eventemitter, hostlistener, oninit } '@angular/core'; import { formcontrol } '@angular/forms'; import { observable, observer, subscription } 'rxjs/rx';  import { cdlservice } './cdl-service';  import { logradourocdl } './logradourocdl';  @directive({     selector: '[mydirective]' }) export class cdldirective implements oninit {     private _searchobservable: any;     private _observer: observer<any>;      @input() other: any;     @output() onresults = new eventemitter<logradourocdl[]>();      @hostlistener('keyup') onkeyup() {         this._observer.next({});     }      constructor(private el: elementref) {          this._searchobservable = observable.create((observer: observer<any>) => {             this._observer = observer;         });          this._searchobservable             .distinctuntilchanged()             .debouncetime(200)             .subscribe(() => {                 this._search();             });      }      ngoninit() {         if (this.numero) {             this.numero.onkeyup = ((evento) => {                 this._observer.next({});             });         }     }      _search() {         // here implemented search logic         // , returned results through event         this.onresults.emit(resultado);     }  } 

this works, i'm wonderer if receive "other" element elementref instead of input.form-controller. if done, wouldn't need use observable fire search method... able subscribe directly elementrefs, this:

observable.fromevent(this.el.nativeelement, 'keyup')             .debouncetime(200)             .distinctuntilchanged()             .subscribe((event) => {                 this._search();             }); 

and

observable.fromevent(this.other **[ elementref.nativeelement ]**, 'keyup')             .debouncetime(200)             .distinctuntilchanged()             .subscribe((event) => {                 this._search();             }); 


Comments