选择框的空值不会给出错误(材料设计)

时间:2016-08-18 12:20:12

标签: javascript jquery angularjs

<md-input-container flex-gt-xs>
    <label translate>rule.type.title</label>
    <md-select name="type" ng-required="true" ng-model="vm.model.type"
               ng-change="vm.onRuleTypeChange(vm.model.type)">
            <md-option ng-repeat="(key,value) in vm.types" ng-value="key">
            <span translate>{{value}}</span>
        </md-option>
    </md-select>
    <div ng-messages="form.type.$error">
        <div ng-message-exp="['required']">
            <span translate>rule.type.errors.required</span>
        </div>
    </div>
</md-input-container>

<md-input-container flex-gt-xs ng-if="vm.isRealTime(vm.model.type)">
    <label translate>rule.result.title</label>
    <md-select name="result" ng-required="true" ng-model="vm.model.result"
               ng-model-options="{trackBy: '$value.id'}">
        <md-option ng-repeat="result in vm.results" ng-value="result">
            <span>{{result.name}}</span>
        </md-option>
    </md-select>
    <div ng-messages="form.result.$error">
        <div ng-message-exp="['required']">
            <span translate>rule.result.errors.required</span>
        </div>
    </div>
</md-input-container>

这是我要问的页面的html。第一个容器用于文本输入,第二个用于选择框。

当我点击保存时,只有输入位置会出错,而不是组合框

when i click save, only input places give errors, not comboboxes

但是当我点击那个boxx并且没有选择任何内容时保留该框时,它会给出错误

but when i click that boxx and leave the box without selecting anything, it gives error

为什么?

很可能是因为

  md-maxlength="50"
               ng-minlength="2"

inpt文本具有该规则,因此它不能具有空值。但是selectbox呢?

1 个答案:

答案 0 :(得分:0)

我做了这个并且有效

<md-input-container flex-gt-xs>
            <label translate>rule.type.title</label>
            <md-select name="type" ng-required="true" ng-model="vm.model.type"
                       ng-change="vm.onRuleTypeChange(vm.model.type)">
                    <md-option ng-repeat="(key,value) in vm.types" ng-value="key">
                    <span translate>{{value}}</span>
                </md-option>
            </md-select>
            <div ng-messages="form.type.$error" ng-show="form.$submitted || form.type.$touched ||  form.type.$dirty ">
                <div ng-message-exp="['required']" ng-hide="form.type.$valid">
                    <span translate>rule.type.errors.required</span>
                </div>
            </div>


        </md-input-container>