即使我添加maxLength =“250”,我也可以在agular2-material md-input 中输入值。
我的表单输入如下:
<div class="form-group">
<md-input type="text" required placeholder="Name" formControlName="name" #nameCountHint
maxLength="250" class="full-width" dividerColor="{{changeForm.controls.name.valid ? 'primary': 'accent'}}">
<md-hint align="end">{{nameCountHint.characterCount}} / 250</md-hint>
</md-input>
<agile-control-messages color="primary" [control]="teamForm.controls.name"></agile-control-messages>
</div>
任何帮助都会很棒。
答案 0 :(得分:1)
我找到了关于如何进行验证的简单演示,也许它可以帮到你。
组件模板:
<form [ngFormModel]="someFormHandle"
[(ngSubmit)="onSubmit()">
<input class="form-control"
[ngFormControl]="someNumber">
<button class="btn btn-primary"
[disabled]="!someFormHandle.valid">
Submit
</button>
</form>
组件:
@Component({
selector: 'some-form',
templateUrl: './some-form.component.html',
directives: [FORM_DIRECTIVES]
})
export class SomeForm implements OnInit {
someFormHandle:ControlGroup;
someNumber:AbstractControl;
constructor(private formBuilder:FormBuilder) {
}
divisibleByTen(control:Control) {
return parseInt(control.value) % 10 == 0 ? null : {
divisibleByTen: true
}
}
onSubmit(){
//Some submit logic
}
ngOnInit():void {
this.someFormHandle = this.formBuilder.group({
'someNumber': ['', Validators.compose([Validators.required,
Validators.maxLength(250),
this.divisibleByTen])]
});
this.someNumber = this.someFormHandle.find('someNumber');
}
}