Angular JS - Automatically focus input and show typeahead dropdown - ui.bootstrap.typeahead

后端 未结 11 1527
孤街浪徒
孤街浪徒 2020-11-30 03:07

I am using Angular JS - ui.bootstrap.typeahead:

I would like to click a button and focus an input field and automatically show the typeahead suggestion dropdown. I h

11条回答
  •  北海茫月
    2020-11-30 03:47

    I got a working solution by changing some code in ui-bootstrap-tpls-0.10.0.js. So there are no differences in the typeahead html markup.

    You can have a look here at http://plnkr.co/edit/LXHDpL?p=preview.

    To use this fix, use the ui-bootstrap-tpls-0.10.0.js from the Plunk. To see my changes, open ui-bootstrap-tpls-0.10.0.js from the Plunk and search for 'ahneo'.

     1. //minimal no of characters that needs to be entered before typeahead
        kicks-in
        // ahneo :: before
        //var minSearch = originalScope.$eval(attrs.typeaheadMinLength) || 1;
        // ahneo :: after (changed minimal no of characters to 0 by default)
        var minSearch = originalScope.$eval(attrs.typeaheadMinLength) || 0;
     2. // ahneo :: new (set input value to empty string if it contains " " string value)
        if (inputValue === ' ') {
            inputValue = '';
            modelCtrl.$setViewValue('');
        }  
     3. // ahneo :: before
        //if (inputValue && inputValue.length >= minSearch) {
        // ahneo :: after (add new condition to get matches for min search = 0)
        if (minSearch === 0 || inputValue && inputValue.length >= minSearch) {
     4. // ahneo :: new (bind element to focus event to trigger modelCtrl.$parsers.unshift method)
        element.bind('focus', function (evt) {
            if (modelCtrl.$viewValue === '') {
                modelCtrl.$setViewValue(' ');
            }
        });
    

    Hope this helps

提交回复
热议问题