-
Notifications
You must be signed in to change notification settings - Fork 27.4k
support blur and focus events #1277
Comments
ToDoMVC wrote angular directives for blur/focus:
Also see other example here: |
ngFocus should definitely not work like this. Angular is based on bidirectional data binding, which means that if the focus attribute is bound to a model, the model should be updated when the element looses focus. An ngFocus which works analogous to the ngBlur directive above would be a good idea. A directive which focuses an element when it becomes visible could be a good idea. |
Any updates on this ? |
Thanks for posting this even if it isn't a great longterm solution. I needed ng-focus too. 👍 |
I'm using the following snippet to support
|
@andershessellund or someone else, could you please give deeper explanation about why model should care about element focus or blur at all? Any HTML element is able to get focus - link or table header cell. From what I understand now, it will be enough if one should be able to trigger 'change' event manually. Also, as far as I know ngChange and ngModel directives are implemented only for input, select, and textarea tags. |
One use-case I can think of is the ability to increase the number of rows in a textarea when it's focused and reduce it when it loses focus. Sorta like the tweet box on twitter. |
Just to clarify, I think that ngFocus and ngBlur should simply bind events to event handlers, like the code by @arcanis . My point was if one was to write a directive that that binds the focus status of an element to a boolean value on the model, it should be two-way. The code snippet posted by @avimar above, which is an ng-focus directive that sets focus on an element when a boolean attribute on the model changes to true should be handled using an event instead. A two-way binding directive could perhaps be implemented, but it would be different than directives such as ng-selected or ng-disabled, because elements do not actually have a 'focused' property. In my opinion, such a directive does not really belong in core angular. A directive that sets focus on an element when it becomes visible could be a good idea. A directive that sets focus on an element when an angular event occurs could also be a good idea. |
+1 |
So I have been working on an implementation of ngFocus that is two way bound to a boolean. I would like to solicit comments and feedback before I bring in a pull request of the same. Here is a plunk which contains an implementation of the same. http://plnkr.co/edit/CvPCVxy4MfJEM1UksrrA?p=preview Points of consideration :
Feedback and comments are welcome. I would like to push this to the core if it is approved. Is this the right way to handle focus and blur? ( we would create ngFocus and ngBlur like ngShow and ngHide ). Are there any problems with this implementation ? |
+1 for @ganarajpr solution |
+1 |
@ganarajpr Solution looks good. Would it work with jqlite if we change |
This seems to work without Jquery as a dependency. |
@fabsor Thank you. I was going to update my plunk with a version that is a bit more advanced. But was not sure if it was necessary or not. Here is a plunk http://plnkr.co/edit/bntEsfngnJKuneg2raD1 This one is without jquery and also there is one additional thing. If the developer assigns the same model to multiple ng-focus directives ( anywhere in the app! ) that means that focus will start jumping between these. I have put in a kind of hack to prevent this. I dont know whether we want to put these in or if there is a more clean way of ensuring that a model is assigned to just one ng-focus. |
@ganarajpr Ugh, a timer doesn't seem like a good solution. |
@fabsor The intersting question is how we could achieve that! If @IgorMinar / @mhevery / @vojtajina have any opinions about this it would be awesome. I think focus and blur are one of the most common things people currently miss in the framework and an opinion in that matter would be great. |
Gist to get ngFocus and ngBlur directives if you need them now https://gist.github.com/eliotsykes/5394631 |
👍 |
2 similar comments
+1 |
+1 |
ganarajpr's solution is working amazingly well. Bravo! |
+1 |
1 similar comment
+1 |
+1 |
5 similar comments
+1 |
+1 |
+1 |
+1 |
+1 |
ganarajpr, it doesn't work for child scopes |
childscope for focus/blur? |
yes |
@8026787 It should work with child scopes as well. It works very similiar to how ng-model works. If you can provide more details as to what you are trying and in what scenario it doesnt work with child scopes it would be helpful. |
Just want to add to the conversation. For my use case, I need to bind to a function and do not require the 2-way data-binding nature of @ganarajpr 's solution. For me @avimar 's solution was a better fit. I'm still just learning Angular. |
I'm sorry, my fault. It works good with child scopes. So the easiest way to get blur event in controller - it's to add the $watch()? I mean if I need to save field data after it losts focus: template: <input ng-model="name" ng-focus="focus.name"> controller: $scope.$watch('focus.name', function(newval){
if(newval===false) onBlur('name')
}) and for every field I need to add separate watch... <input ng-model="name" ng-focus="onFocus('name')"> |
@IgorMinar / @mhevery / @vojtajina, is there a particular reason for not including these events ? I saw that you mentionned them in the #314, but they seem to have been forgotten since then :) |
+1 |
@arcanis I've been using your solution for a while now and it's great! The only issue is that |
+1 |
+100,000 |
+1 for arcanis's solution. This should really be in the default API. |
If the purpose is setting the focus or causing a blur on a specific element, @ganarajpr has the best solution. I am using this directive and am happy with it, but there are no unit tests for it, and I am going to end up writing them myself unless the author posts some. There is also a good checklist for submitting a PR to be found here: #2979. If the purpose is reacting to a focus or blur event, the folks at AngularUI have a very good solution: https://github.com/angular-ui/ui-utils/blob/master/modules/event/event.js. I use this module in several places and am very happy with it. |
@ganarajpr thanks +1 |
Read only support for blur / focus events just landed in master. Let's move the discussion about setting focus to #2012 |
Added directives for focus and blur events. Closes angular#1277
for anyone else wondering where exactly the commit landed: 2bb27d4 |
@ganarajpr: thanks for your solution, it works well enough given how hard Angular makes this kind of thing. (The hack for detecting multiple bound elements is an abomination though.) An issue I have with setting focus in JavaScript with .focus() in general is that it doesn't behave the same as when the user sets the focus on a text field (when jumping between fields or clicking on a label): when the user jumps to a field, the field automatically gets selected, but .focus() doesn't .select() the text; it doesn't even focus to the end of the text. I need consistent behavior for this. So whichever directive eventually gets added to Angular should either implement "focus and select" or should allow to customize what happens. |
@andreas-gruenbacher yes having the same issue here. i've workarounded it by also sending a "cklick" event. |
Almost all events are supported by angularjs but blur and focus events are left out. please add support for this.
The text was updated successfully, but these errors were encountered: