angular - How to set focus on element with binding? -
in angular2 how can set binding on element focus. don't want set elementref. think in angularjs there ngfocus directive in angular2 there no such directive
a simple 'focus' directive
import {directive, input, elementref} 'angular2/core'; @directive({ selector: '[focus]' }) class focusdirective { @input() focus:boolean; constructor(@inject(elementref) private element: elementref) {} protected ngonchanges() { this.element.nativeelement.focus(); } } // usage @component({ selector : 'app', template : ` <input [focus]="inputfocused" type="text"> <button (click)="movefocus()">move focus</button> `, directives: [focusdirective] }) export class app { private inputfocused = false; movefocus() { this.inputfocused = true; // need because nothing // happens on next method call, // ngonchanges in directive called if value changed, // have reset value in async way, // ugly works settimeout(() => {this.inputfocused = false}); } }
Comments
Post a Comment