2017-06-01 56 views
0

當我用md-separator-keys使用md-chips時,它可以工作。 如果我添加md-autocomplete,那麼md-separator-keys不起作用。如何使md-chips md-separator-keys與md-autocomplete一起工作?

這是我如何使用它:

<md-chips ng-model="chips.selectedChips" md-add-on-blur="true" md-separator-keys="customKeys" 
      md-transform-chip="chips.transformChip($chip)" 
      md-require-match="chips.allowToAddNew" md-autocomplete-snap> 
    <md-autocomplete 
     md-selected-item="chips.selectedItem" 
     md-search-text="chips.searchText" 
     md-items="item in chips.querySearch(chips.searchText)" 
     md-item-text="chips.getText(item)" 
     placeholder="{{ field.hint }}"> 
    <span md-highlight-text="chips.searchText">{{ chips.getText(item) }}</span> 
    </md-autocomplete> 
    <md-chip-template> 
    <span>{{ chips.getText($chip) }}</span> 
    </md-chip-template> 
</md-chips> 

customKeys初始化是這樣的:

scope.customKeys = [ $mdConstant.KEY_CODE.ENTER, $mdConstant.KEY_CODE.COMMA, 186 ]; 

的問題是:如何使它工作?

p.s.出於某種原因,模糊附加功能無法正常工作,因此找到如何解決該問題也不錯。

p.p.s.我在github上發現了關閉issue,所以他們不打算解決這個有效問題。迷人。

回答

0

好的。我知道這不是一個完美的解決方案,而是一種解決方法。 這是我已經添加到我的自定義指令的鏈接方法:

scope.$watch('chips.searchText', function(newValue, oldValue) { 
    if (!newValue || newValue.length < 2) { 
     return; 
    } 
    if (";,".indexOf(newValue.substring(newValue.length - 1)) < 0) { 
     return; 
    } 

    var chipName = newValue.substring(0, newValue.length - 1); 
    if (!scope.chips.selectedChips) { 
     scope.chips.selectedChips = []; 
    } 
    scope.chips.selectedChips.push(scope.chips.transformChip(chipName)); 
    scope.chips.searchText = ''; 
}); 

而且我刪除md-separator-keysmd-chips,因爲它成爲完全是多餘的