Buduję formularz z ControlGroup i ładuję obiekt klasy w nim. Jednak napotykam błąd wspomniany w tytule w połowie czasu. Niektóre formularze ładują się, a niektóre nie.Angular 2 Maszynopis: TypeError: this.validator nie jest funkcją
Mam plik klasy tak:
export class User {
id: number;
email: string;
sign_in_count: number;
created_at: string;
first_name: string;
last_name: string;
birth_date: Date;
news_letter: boolean;
fb_id: string;
gender: boolean;
phone: string;
picture: any;
}
W moim UserDetailComponent załadować klasę w kontroli jak ten:
export class UserDetailComponent implements OnInit {
user: User;
userDetailForm: ControlGroup;
constructor(
private form: FormBuilder,
private _userService: UserService,
private _router: Router,
private params: RouteSegment
) { }
ngOnInit() {
this.user = this._userService.getUser();
if (this.user === undefined) {
this._userService.getSingleUser(this.params.getParam('id'))
.subscribe(data => (this.user = data, this.setForm()));
} else {
this.setForm();
}
}
setForm() {
this.userDetailForm = this.form.group(this.user);
}
}
Na tym ostatnim wierszu pojawia się błąd, którego stacktrace jest poniżej:
browser_adapter.ts:78 TypeError: this.validator is not a function
at Control.AbstractControl._runValidator (model.ts:146)
at Control.AbstractControl.updateValueAndValidity (model.ts:128)
at new Control (model.ts:282)
at FormBuilder.control (form_builder.ts:32)
at FormBuilder._createControl (form_builder.ts:66)
at eval (form_builder.ts:50)
at Function.StringMapWrapper.forEach (collection.ts:132)
at FormBuilder._reduceControls (form_builder.ts:49)
at FormBuilder.group (form_builder.ts:19)
at UserDetailComponent.setForm (user-detail.component.ts:95)
Może być konieczne utworzenie formularza w konstruktorze i zaktualizowanie wartości (lub dodanie/usunięcie elementów) tylko wtedy, gdy dotrą dane z 'UserService'. –
Co to jest FormBuilder? Czy to jest import? –
@StianStandahl tak to jest import @ kątowy/wspólny - https://angular.io/docs/ts/latest/api/common/FormBuilder-class.html – Ruben