正如其他人所说,在您的输入中添加“名称”属性。
我使用 ng-message 制作了一个自定义指令来显示输入错误(所以你必须包含它)
directives.directive('inputerrormsg', function () {
return {
replace: true,
restrict: 'E',
plain: true,
scope: {
inputdata: "=",
dpattern: '@'
},
template: '<div class="help-block has-error text-center" ng-messages="inputdata[\'$error\']" >' +
'<p ng-message="required">{{\'The field is required\'| translate }}</p>' +
'<p ng-message="minlength">{{\'Input too short\'| translate }}</p>' +
'<p ng-message="maxlength">{{\'Input too long\'| translate }}</p>' +
'<p ng-message="email">{{\'Email invalid\'| translate }}</p>' +
'<p ng-message="date">{{\'Date invalid\'| translate }}</p>' +
'<p ng-message="number">{{\'Write number only\'| translate }}</p>' +
'<p ng-message="pattern">' +
'<span ng-switch="dpattern">' +
'<span ng-switch-when="date">{{\'Date incorrecte: YYYY, YYYY/MM, YYYY/MM/DD\' |translate}}</span>' +
'<span ng-switch-default>{{\'Saisir uniquement des lettres\'| translate }}</span>' +
'</span>' +
'</p>' +
'</div>'
}
});
用法 :
必填项目
<div style="padding-left: 0px;" ng-class="{ 'has-error' : !formDeter.scientificname.$valid">
<div class="input-group no-padding">
<input type="text"
class="form-control input-md" name="scientificname"
required />
<span class="input-group-addon">
<span class="glyphicon glyphicon-question-sign"></span>
</span>
</div>
<dir.inputerrormsg inputdata="formDeter.scientificname" ></dir.inputerrormsg>
电子邮件 :
<div class="form-group row" ng-class="{ 'has-error' : !formData.email.$valid }">
<label class="col-xs-4 control-label">{{"Email" | translate }}</label>
<div class="col-xs-8 input-group">
<input type="email" class="form-control input-md" ng-model="email" name="email" id="email" required
placeholder="{{'Email' | translate }}"/>
</div>
<dir.inputerrormsg inputdata="formData.email" dpattern="email" ></dir.inputerrormsg>
</div>
仅数字:
<div class="form-group row" ng-class="{ 'has-error' : !formLocalisation.decimallongitude.$valid }">
<label class="col-xs-4 control-label">{{"Longitude" | translate }}</label>
<div class="col-xs-8 input-group">
<input type="number" popover-trigger="focus" placeholder="{{'Longitude' | translate }}"
class="form-control input-md"
ng-model="specimen.decimallongitude" name="decimallongitude"
/>
<span class="input-group-addon" >
<span class="glyphicon glyphicon-question-sign"></span>
</span>
</div>
<dir.inputerrormsg inputdata="formLocalisation.decimallongitude" ></dir.inputerrormsg>
</div>
...