如何在md-autocomplete + md-chip中进行验证

时间:2017-11-21 12:28:54

标签: angularjs validation angular-material md-autocomplete md-chip

我需要在自动完成功能中进行验证,例如

  • min length
  • 最大长度
  • 芯片总数

我找到了验证所有这些验证的正确方法 我的HTML是

<md-chips md-on-add="selectedGroups.add($chip)" ng-focus="userTag"
                    ng-model="selectedGroups" is="tagSelect" md-autocomplete-snap
                    md-require-match="true" md-max-chips="3" maxlength="10">
                <md-autocomplete 
                    md-search-text="searchText"
                    md-items="item in queryGroups(searchText)" 
                    md-item-text="item"
                    md-autoselect="true" md-select-on-match="true"
                    md-no-cache="true"
                    md-require-match="true"
                    md-input-name="autocompleteField"
                    md-input-minlength="2"
                    md-input-maxlength="5"
                    >
                     <span
                    md-highlight-text="searchText">{{item}}</span>
                    <div ng-messages="searchForm.autocompleteField.$error" ng-if="searchForm.autocompleteField.$touched">
                <div  ng-message="required">You <b>must</b> have a favorite state.</div>
                <div  ng-message="minlength">Your entry is not long enough.</div>
                <div  ng-message="maxlength">Your entry is too long.</div>
              </div>
                     </md-autocomplete> 
                    <md-chip-template
                    ng-maxlength="2" md-max-chips="5" required> <span>{{$chip}}</span>
                </md-chip-template> </md-chips>

我的js是

   var allGroups = ['one','two','three'  ];
$scope.queryGroups = function(search) {

    var firstPass = $filter('filter')($scope.allGroups, search);
       return firstPass.filter(function(item) {
        return $scope.selectedGroups.indexOf(item) === -1;
        console.log(item)
    });

};

请帮帮我

1 个答案:

答案 0 :(得分:1)

  • 芯片总数

    对于芯片的总数,使用md-max-chips(这是你的最大数量+1让我想要5个芯片,然后我使用md-max-chips =“6”)在你的md-chips指令中它将如果达到最大值,则禁用输入。如果你想要一些错误输出,你可以在你的筹码指令下创建一个跨度,如果在模型的最大长度上为ng-if(ng-if =“model.length&gt; = 6)

  • 输入的最小/最大长度

    这是一个小技巧,因为你想使用自动完成,md-maxlength不存在。所以你可以做的就是在文本更改中对字符串进行子串,以便它永远不会超过5个字符(md-search-text-change =“text_model = text_model.substring(0,5)”)

    <md-chips
     name="label"
     ng-model="selectedGroups"
     md-max-chips="3"
     md-removable="true"
     md-on-add="someCheckOnAdd($chip)"
     md-on-remove="someCheckOnDelete($chip)">
       <md-autocomplete 
         md-search-text="text_model"
         ng-disabled="selectedGroups.length >= 3"
         md-items="item in queryGroups(text_model)"
         md-item-text="item"
         md-autoselect="true"
         md-search-text-change="text_model = text_model.substring(0,5)"
         md-select-on-match="true"
         md-no-cache="false"
         md-require-match="true">
           <span md-highlight-text="searchText">{{item}}</span>
       </md-autocomplete>
            <md-chip-template >
                <strong style="padding-right: 10px;">{{$chip}}</strong>
            </md-chip-template>
    </md-chips>
    
    <span ng-if="model.length > 7" class="alert-message-edition-match">Max chips number reached</span>
    

这是一个适合你想做的工作fiddle