Material: ng-focus on md-autocomplete

Created on 27 Jul 2015  路  60Comments  路  Source: angular/material

When focus set to md-autocomplete, ng-focus function not call.

minor

Most helpful comment

I've improved a bit the workaround provided by @KarmaCop213:

(function() {
  'use strict';

  angular
  .module('app')
  .directive('mdBlur', function($timeout) {
    var directive = {
      restrict: 'A',
      link: function(scope, element, attributes){
        $timeout(function(){
          angular.element(element[0].querySelector("input.md-input")).bind("blur", function(){
            $timeout(function() {
               scope.$eval(attributes.mdBlur);
            }, 100);
          });
        },0);
      }
    };

    return directive;
  });

})();

Just change yourCallback with whatever you want.

<md-autocomplete
   (...)
   md-floating-label="Some text"
   md-blur="yourCallback()">
   (...)
</md-autocomplete>

All 60 comments

Same issue applies to ng-blur as well.

+1
Perhaps the ng-onblur on md-autocomplete have to be $eval on ng-onblur of internal input.
Actually the internal input have ng-blur="$mdAutocompleteCtrl.blur()"

Te same for ng-focus.

Bumping this to 0.12 since more users have been asking for it and it should be relatively straightforward to implement.

+1

A simple modification to the autocompleteDirective and autocomepleteController to expose a directive attribute, something like md-focus and md-blur would allow consumer controllers to operate on those events. as @tolemac pointed out, the internal input already has ng-focus and ng-blur implemented. Hooking in attribute expressions using $parse should be quite simple to implement.

I have a standing PR in https://github.com/angular-ui/ui-select that does something similar: https://github.com/angular-ui/ui-select/pull/1153

+1 for md-blur on md-autocomplete

+1

+1

+1!

+1

+1

+1

+1

+1

+1

+1

What I did to still have an "onBlur" event fired:

Created a directive called "autocompleteBlur", the $timeout is important, otherwise the input textbox isn't rendered at that time.

allocateApp.directive('autocompleteBlur', function($timeout){
    return{
        link: function(scope, element, attrs){
            $timeout(clearInputBoxOnBlur, 0);

            function clearInputBoxOnBlur(){
                element.find("#autocomplete-input-id").bind("blur", function(){
                    angular.element(this).val('');
                });
            }
        }
    }
});

Then on the md-autocomplete element, add the newly created directive, and give an id to your input textbox (mine is called "autocomplete-input-id"):

<md-autocomplete
    (...)
    autocomplete-blur
    md-input-id="autocomplete-input-id">
    (...)
</md-autocomplete>

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

any update on this ticket, or new work arounds?

+1

LOL... I'm so glad that I've moved on to React: http://www.material-ui.com/#/components/app-bar

I'm not sure why this is marked as an edge-case. Consider the use-case where you want to allow the user to enter _any_ input into a text field, but allow them some pre-defined options via autocomplete. Right now there is a strict separation between searchText and modelValue.

If searchText === modelValue _and_ there is an exact match in the matches list then you'll have a hard time. The matches list will _not_ display because $mdAutocompleteCtrl.hidden will be true. It isn't difficult to create a scenario. Consider that the user types something like 3m into the input. The matches are: 3m, 3M, 3M ESPE. The searchText is 3m, so it matches something in the list. $mdAutocompleteCtrl thinks that it's time to pack everything up, so it renders the list of matches, and hides them from view.

(I'm specifically referring to shouldHide calling hasSelection here.)

Now consider that you want the user to be able to enter 3m2m and save it. My first thought to work around this would be to use ngBlur to copy searchText to modelView when the user explicitly leaves the autocomplete. Without ngBlur though this will not work.

Now I'm sure that "using it as a work around" is your target for features, but by giving users a broader set of tools to work with they can at least work around issues that are not entirely ironed out yet rather than ending up waiting for a fix to the issues, or maintaining their own fork (neither of which are entirely palatable options).

It can be even more common. Imagine you want to show something when the user is editing the auto-complete field, and hide it once he's done. ngBlur would be really handy.

+1 for ng-focus

+2

+1

When a user moves away from a field I need to run a function to update the available lists of other items in a form.

I don't understand how this is an edge case when so many people are +1ing it and it clearly has thousands of use-cases.

+1

I've improved a bit the workaround provided by @KarmaCop213:

(function() {
  'use strict';

  angular
  .module('app')
  .directive('mdBlur', function($timeout) {
    var directive = {
      restrict: 'A',
      link: function(scope, element, attributes){
        $timeout(function(){
          angular.element(element[0].querySelector("input.md-input")).bind("blur", function(){
            $timeout(function() {
               scope.$eval(attributes.mdBlur);
            }, 100);
          });
        },0);
      }
    };

    return directive;
  });

})();

Just change yourCallback with whatever you want.

<md-autocomplete
   (...)
   md-floating-label="Some text"
   md-blur="yourCallback()">
   (...)
</md-autocomplete>

+1

+1

hey.. All of you commuting +1.. I cannot understand how you solve this issue. :(

+1

+10

+1

Can we get a dev to actually give us an update on this issue? The ticket has been open for 9 months, there hasn't been one comment by a dev other then to lower the priority or reschedule looking at this issue. Every month there is at least one more +1, and who knows how many people just read this thread and give up because of the poor communication from the devs.

All I want to know is are you ACTIVELY working on this issue or is this ticket just going to remain on the sidelines forever?

+1

+1

A million +1s, and the ticket gets put in ... deprecated? Without a reason why?

It would awful nice to have ng-focus and ng-blur work. Why is this deprecated? Do you need a pull request?

@tim93422 It's not deprecated anymore.

Pull Requests are welcome as always, but please discuss the possible solutions first.

I'm not really in the position propose a solution yet. If you have a fruitful avenue for me to explore, please let me know. If not, I'll put some time into understanding the issue more fully

+1

+1

+1

+1

+1

Here is my workaround to override blur function of the autocomplete controller

app.directive('mdBlur', [ "$mdUtil", function($mdUtil) {
    return {
        require: "^mdAutocomplete",
        link: function($scope, $element, $attributes, $mdAutocompleteCtrl) {
            $timeout(function() {
                var input = $element.find("input");
                var element =  $element[0];
                var nativeBlur = $mdAutocompleteCtrl.blur;
                $mdAutocompleteCtrl.blur = function() {
                    nativeBlur.call($mdAutocompleteCtrl);
                    $mdUtil.nextTick(function () {
                        $scope.$eval($attributes.mdBlur, {"$mdAutocomplete": $mdAutocompleteCtrl });
                    });
                };
            });
        }
    };
} ]);

+1

+1

This issue is closed as part of our 鈥楽urge Focus on Material 2' efforts.
For details, see our forum posting @ http://bit.ly/1UhZyWs.

:+1: ng-focus on md-autocomplete

@all @here
ngBlur and ngFocus is start working from 1.1.1 version

+1

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rdantonio picture rdantonio  路  3Comments

ghost picture ghost  路  3Comments

kasajian picture kasajian  路  3Comments

nikhildev picture nikhildev  路  3Comments

buzybee83 picture buzybee83  路  3Comments