11
Tôi vẫn còn mới và học qua hướng dẫn, nhưng vì một lý do nào đó, tôi nhận được thông báo No provider for formbuilder
khi cố gắng kiểm tra tên người dùng duy nhất. Tôi không chắc tại sao tôi nhận được thông báo này, nhưng tôi không thể tìm được giải pháp. Bất cứ ai có thể cho tôi biết lý do tại sao điều này xảy ra?Không có nhà cung cấp nào cho FormBuilder
signup-component.ts
import {Component} from '@angular/core';
import {ControlGroup, Control, Validators, FormBuilder} from '@angular/common'
import {UsernameValidators} from './usernameValidators'
@Component({
selector: 'signup',
templateUrl: 'signup-component.html'
})
export class SignupComponent{
form: ControlGroup;
constructor(fb: FormBuilder){
this.form = fb.group({
username:['', Validators.compose([
Validators.required, UsernameValidators.cannotContainSpace
])],
password: ['', Validators.required]
})
}
}
usernameValidators.ts
import {Control} from '@angular/common'
export class UsernameValidators{
static shouldBeUnique(control: Control){
return new Promise((resolve, reject) => {
setTimeout(function(){
if(control.value == "andy")
resolve({shouldBeUnique: true});
else
resolve(null);
}, 1000);
});
}
static cannotContainSpace(control: Control){
if (control.value.indexOf(' ') >= 0)
return {cannotContainSpace: true};
return null;
}
}
Cảm ơn! nó đã làm việc, tôi đã nhập ReactiveFormsModule trong app.module.ts vì nó thiếu – sabin
ReactiveFormsmodule và FormsModule không nên được nhập cùng nhau trong cùng một mô-đun – DAG
@dag Tại sao? '6 nữa để đi ...' – tom10271