2016-08-23 35 views
0

我想限制一些固定的標籤,然後後不能添加新的標籤角NG-標籤 - 輸入最大標籤不工作

<tags-input ng-model="searchTerm" 
      display-property="Name" 
      add-from-autocomplete-only="true" 
      replace-spaces-with-dashes='false' 
      max-tags='4' 
     placeholder="Search option" 
      > 
    <auto-complete source="loadTags($query)" min-length='3'></auto-complete> 

我添加了NG-標籤輸入JS也盡需要改變什麼是最大標籤的問題?

+1

'MAX-tags'的默認行爲解決辦法是改變NG-模式進入'invalid'狀態,並添加'NG-invalid'類輸入,如果你想限制用戶輸入新標籤,爲此創建'custom directive',因爲這個功能還沒有在生產中https://github.com/mbenford/ngTagsInput/issues/210 –

回答

0

使用自定義指令

app.directive('enforceMaxTags', function() { 
     return { 
      require: 'ngModel', 
      link: function(scope, element, attrs, ngModelCtrl) { 

       var maxTags = attrs.maxTags ? parseInt(attrs.maxTags, '10') : null; 

       ngModelCtrl.$validators.checkLength = function(value) { 

        if (value && maxTags && value.length > maxTags) { 
         value.splice(value.length - 1, 1); 
        } 
        return value ? value : []; 
       }; 
      } 
     }; 
    }); 

https://github.com/mbenford/ngTagsInput/issues/210