我只是問一些關於驗證使用模式的角度2.使空間驗證程序Angular2
這張照片顯示我的網頁。在紅色框中,我想設置驗證。 我的驗證是這個文本框不能輸入空間並進行修剪。只是簡單,但我不能這樣做。
ngOnInit() {
this.submitButtonText = 'Save';
this.form = this.fb.group({
'id': [],
'parent': ['-1', Validators.compose([Validators.required])],
'name': ['', Validators.compose([Validators.required])],
'title': ['', Validators.compose([Validators.required])],
'router': ['/apps'],
'description': ['', Validators.compose([Validators.required])]
});
this.parent = this.form.controls['parent'];
this.name = this.form.controls['name'];
this.title = this.form.controls['title'];
this.router = this.form.controls['router'];
this.description = this.form.controls['description'];
this.toggleFormControl("router",false);
this.service.getParent()
.subscribe(
data => this.setParent(data['menu']),
err => this.logError(err),
() => this.finishedGetIndexList('Save')
);
this.statusForm = 'Add';
if (this.id) {
this.fetchData();
}
}
的HTML是
<div class="form-group row" [ngClass]="{'has-error': (!name.valid && name.touched), 'has-success': (name.valid && name.touched)}">
<label class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input [formControl]="name" type="text" class="form-control" placeholder="name"/>
</div>
</div>
的功能幾乎是正確的,但你的函數被反轉,你的功能給我驗證的文本框必須具有的空間,但我想是我的文本框不能輸入空格。 –
Jep,你說得對。我改變了,謝謝。我沒有測試代碼。如果這對你有幫助,請將其標記爲已接受的答案。 – LanderV
我無法修復。請更正此代碼。之後,我將標記此 –