7

重新打開結果下拉我有問題在預輸入以下用例:AngularJS [預輸入]上的onfocus

問題:

  • 用戶開始輸入,下拉打開並顯示結果
  • 用戶點擊在輸入字段外(沒有任何東西從輸入字段中刪除),下拉框關閉
  • 用戶再次回到輸入字段(並且不會開始鍵入任何內容)並且什麼都不會發生。 →期望的行爲是:下拉菜單再次打開並顯示與上次相同的結果列表(當然,只有在輸入字段中有任何內容時纔會發生)
  • 當用戶搜索任何內容並且如果發現結果並且發生焦點丟失並且字符串不會再被清除焦點沒有任何事情發生,沒有提示
  • 當用戶搜索任何東西,並且如果找不到結果並且發生焦點丟失字符串被清除並且還有沒有找到數據的消息。

所以,這個故事的寓意是,在第一種情況下字符串,如果它不是從列表中選擇,它是名單串子應該得到清除...

所以,也許typeahead-搜索焦點設置?

HTML:

<input type="text" focus-me="opened" ng-focus="onFocus($event)" ng-show="opened" ng-trim="false" ng-model="selected" empty-typeahead typeahead="state for state in states | filter:$viewValue:stateComparator" class="form-control" /> 

JS:

(function() { 
var secretEmptyKey = '[$empty$]' 

angular.module('plunker', ['ui.bootstrap']) 
.directive('focusMe', function($timeout, $parse) { 
    return { 
     //scope: true, // optionally create a child scope 
     link: function(scope, element, attrs) { 
      var model = $parse(attrs.focusMe); 
      scope.$watch(model, function(value) { 
       if(value === true) { 
        $timeout(function() { 
         element[0].focus(); 
        }); 
       } 
      }); 
     } 
    }; 
}) 
.directive('emptyTypeahead', function() { 
    return { 
    require: 'ngModel', 
    link: function (scope, element, attrs, modelCtrl) { 
     // this parser run before typeahead's parser 
     modelCtrl.$parsers.unshift(function (inputValue) { 
     var value = (inputValue ? inputValue : secretEmptyKey); // replace empty string with secretEmptyKey to bypass typeahead-min-length check 
     modelCtrl.$viewValue = value; // this $viewValue must match the inputValue pass to typehead directive 
     return value; 
     }); 

     // this parser run after typeahead's parser 
     modelCtrl.$parsers.push(function (inputValue) { 
     return inputValue === secretEmptyKey ? '' : inputValue; // set the secretEmptyKey back to empty string 
     }); 
    } 
    } 
}) 
.controller('TypeaheadCtrl', function($scope, $http, $timeout) { 
    $scope.selected = undefined; 
    $scope.states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Wyoming']; 
    $scope.opened = true; 

    $scope.stateComparator = function (state, viewValue) { 
    return viewValue === secretEmptyKey || (''+state).toLowerCase().indexOf((''+viewValue).toLowerCase()) > -1; 
    }; 

    $scope.onFocus = function (e) { 
    $timeout(function() { 
     $(e.target).trigger('input'); 
     $(e.target).trigger('change'); // for IE 
    }); 
    }; 
}); 
}()); 

我有這個Plunker展示我的代碼看起來像。

還有GitHub Issue

回答

0

大家好,

經過一番努力,我得到了妥善解決。 ..

有一些指令,可以是用於溶液是有用的,

添加上述條件在typeaheadFocus指令,

if ((e.type == "focus" && viewValue != ' ') && (e.type == "focus" && viewValue != '')){ 
     return; 
    } 

添加以下在typeaheadFocus指令和typeaheadOnDownArrow指示功能,

//compare function that treats the empty space as a match 
    scope.$emptyOrView = function(actual) { 
     if(ngModel.$viewValue.trim()) { 
      return actual ? actual.value.toString().toLowerCase().indexOf(ngModel.$viewValue.toLowerCase()) > -1 : false; 
     } 
     return true; 
    };    

改變這種狀況在UI-bootstrap.js,

if (inputFormatter) { 
    locals.$model = modelValue; 
    if(modelValue){ 
    locals.$label = view_value; 
    } else { 
    locals.$label = ''; 
    } 

添加以下的用戶界面,引導JS event

element.bind('blur', function(evt) { 
    hasFocus = false; 
    if (!isEditable && modelCtrl.$error.editable) { 
     element.val(''); 
     modelCtrl.$viewValue = ' '; 
    } 
    if(!isEditable && scope.no_data_found) { 
     //ngModel.$viewValue = ''; 
     modelCtrl.$viewValue = ' '; 
     scope.no_data_found = false; 
     popUpEl.find('.typeaheadNoData').remove(); 
     resetMatches(); 
     scope.$digest(); 
     element.val(""); 
    } 
}); 

在UI的bootstrap.js添加這個,

 // I listen for emptyTypeAhead events from the parent scope chain. 
    originalScope.$on(
     "emptyTypeAhead", 
     function handlePingEvent(event, newVal1) { 
      var scope = originalScope.$new(); 
      modelCtrl.$setViewValue(newVal1); 
      return newVal1; 
     } 
    ); 

添加與UI的bootstrap.js上述功能中的所有指令,

.directive('shouldFocus', function($parse) { 
    return { 
     restrict: 'A', 
     link: function(scope, element, attrs) { 
      scope.$watch(attrs.shouldFocus, function(newVal, oldVal) { 
       var isActive = scope.$eval(attrs.shouldFocus); 
       if(isActive) { 
        var ele = element[0]; 
        var parent = ele.parentNode 

        if(ele.offsetTop + ele.offsetHeight > parent.offsetHeight + parent.scrollTop){ 
         parent.scrollTop = ele.offsetTop; 
        } else if(parent.scrollTop > ele.offsetTop) { 
         parent.scrollTop = ele.offsetTop; 
        } 
       } 
      }); 
     } 
    } 
}); 

例子:

<input type="text" ng-model="inquiry.account" 
placeholder="Select Account" 
typeahead="account.id as account.text for account in account_typeahead_json" 
typeahead-input-formatter="formatModel($model,$label)" 
typeahead-editable="false" typeahead-on-down-arrow typeahead-focus /> 

希望這個答案對你有所幫助。

Here我已添加ui-bootstrap.js

0

您需要檢查模糊輸入值是否存在於列表中。 對於執行以下操作:輸入

添加屬性ng-blur="onBlur($event)"而在你的控制器定義如下方法:

$scope.onBlur = function (e) { 
    $timeout(function() { 
    var val = $(e.target).val(); 
    if(val && $scope.states.indexOf(val) == -1) { 
     $(e.target).val("") 
    } 
    }); 
};