-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Support HTML5 autofocus attribute #10833
Comments
A previous topic about this was closed as "not core": #2012 (comment) I agree with the reasoning, but I'll keep it open for further opinions. |
So |
In that sense, any website that uses JavaScript to insert page elements "breaks" the autofocus attribute. |
@Narretz sure, but Angular has the opportunity to fix it... Declarative focus is so much nicer than imperative. It keeps the code clean, no having to access the DOM from controllers. |
I think that supporting attribute "autofocus" should be done in a better way in Angular, with binding. It happens way too often that one needs to autofocus elements conditionally. For example, a default autofocus element can become disabled or invisible and you need to change autofocus accordingly. The basic HTML5 support for it is just too primitive. Why not do it right from start and use the power of Angular to autofocus with support for optional one-way binding? We need ng-autofocus={{boolean condition}}, to trigger focus change when a certain condition is met. When used with once-off binding, ng-autofocus={{::condition}}, it would be similar to autofocus attribute when the condition is true. And in general it would let us handle automatic focus change in response to changes within a form. It would be used mostly through once-off binding, as this: ng-focus={{::condition}}. |
I'm not sure I agree, the autofocus attribute has the intent of specifying The stateful focus management you propose has already been decided as not
|
+1 |
2 similar comments
+1 |
+1 |
+1 |
+1 I would not have expected Angular to break HTML in such a basic and unnecessary way. 😒 |
@9662 How exactly does angular break HTML? It's a spec problem if the autofocus attribute does not work with dynamically generated elements. |
I agree with the reason to close the previous issue. It is very easy to create a directive on the user side to do autofocusing if one wants it. |
http://caniuse.com/#feat=autofocus tells the browser to focus an element on page load. It would be great if Angular had support for this built-in, so that it also works on browsers that don't support it and when creating elements dynamically, like in forms.
As an example, here's a directive that does this: https://gist.github.com/mlynch/dd407b93ed288d499778
Perhaps this can be used as-is, or it might need adapting for the case where many elements with the
autofocus
attribute get created at once (per the spec only one such element should exist per doc but hey).The text was updated successfully, but these errors were encountered: