Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I have an AngularDart component, how to get the text input field to auto-focus every time the component shows?

I have a text input in a AngularDart component like the following:

<input type="email" id="inputFirstName" ng-model="cmp.inputFirstName">

How can I make the text input auto-focus every time the component shows?

I tried setting the html5 attribute autofocus, but that only works on the first time the component is displayed.

like image 392
Phil Avatar asked Apr 27 '14 09:04

Phil


1 Answers

You could try to use a custom Directive (new Decorator) for this:

import 'package:angular/angular.dart' as ng;
import 'dart:html';

@ng.Decorator(selector: '[autofocus]')
class AutoFocusDecorator implements ng.AttachAware{
  InputElement inputElement;

  AutoFocusDecorator(Element this.inputElement);

  @override
  void attach() {
    inputElement.focus();
  }
}

and use it like

<input type="email" id="inputFirstName" ng-model="cmp.inputFirstName" autofocus>
like image 170
Günter Zöchbauer Avatar answered Oct 21 '22 18:10

Günter Zöchbauer