问题
I have a form that its fields are inside the component. Now I want to validate the fields. The problem is, my messages don't show up. also the pattern doesn't have any effects. I tried to fix it with help of this post, but no success.
Does someone have any idea how can I show my messages?
index.html
<form name="dataForm">
<md-tabs>
<valid-data form-reference="dataForm"></valid-data>
</md-tabs>
<div>
<md-button type="submit" ng-click="vm.save()"> Save </md-button>
</div>
</form>
component.html
<md-tab label="Info Data">
<md-content class="md-margin">
<div layout-gt-sm="row">
<md-input-container class="md-block" flex-gt-sm>
<label>Name</label>
<input ng-model="vm.data.name" name="name" required
ng-minlength="10"
ng-maxlength="20"
pattern="{{validation.validateWord}}">
<div ng-messages="formReference.name.$error" ng-if="formReference.name.$touched">
<div ng-message="required">Name is required.</div>
<div ng-message="minlength">minLength Error msg.</div>
<div ng-message="maxlength">maxLength Error msg.</div>
<div ng-message="pattern">Invalid characters.</div>
</div>
</md-input-container>
</div>
</md-content>
</md-tab>
This is the pattern:
$rootScope.validation = {
'validateWord': '^\\w+(-\\w+)*$', // 0-9, a-z, A-Z, underscores + dashes
};
component.controller.js
bindings: {
formReference: '<'
}
来源:https://stackoverflow.com/questions/45593764/how-to-validate-form-in-angularjs-1-x-component