我在formGroup
面临问题。首先基于URL我取一些值并调用API来检索前场文本的特定用户数据。
register.html
<form [formGroup]="form" (ngSubmit)="onSubmit(form.value)" class="form-horizontal">
<div class="form-group row">
<label for="inputEmail3" class="col-sm-4 ">Username</label>
<div class="col-sm-8">
<input [formControl]="email" type="text" class="form-control" id="inputEmail3" placeholder="Email Address" [readonly]="isReadOnly">
</div>
</div>
</form>
register.component.ts
import { Component } from '@angular/core';
import { FormGroup, AbstractControl, FormBuilder, Validators } from '@angular/forms';
import { Router, ActivatedRoute } from '@angular/router';
import { EmailValidator, EqualPasswordsValidator } from '../../theme/validators';
@Component({
selector: 'register',
templateUrl: './register.html',
})
export class Register {
public form: FormGroup;
public email: AbstractControl;
public username: string;
constructor(private registerService: RegisterService, fb: FormBuilder, private router: Router, private route: ActivatedRoute) {
this.form = fb.group({
'email': ['', Validators.compose([Validators.required])]
.... etc..
});
this.email = this.form.controls['email'];
this.registerService.getUser({ userId: "asdasd2123da2das" }).subscribe(posts => {
if (posts) {
var userObj = posts.json();
console.log("userObj : ", userObj.data);
if (userObj.data && userObj.data[0].email) {
this.email = this.username = userObj.data[0].email; // ouput : abc@gmail.com
this.isReadOnly = true;
this.router.navigateByUrl('/register');
} else {
alert("You are Not Autorize to access this Page");
this.router.navigateByUrl('/login');
}
}
});
}
}
错误详情:
TypeError: Cannot create property 'validator' on string 'abc@gmail.com'
at setUpControl (http://localhost:3004/vendor.dll.js:9739:23)
at FormControlDirective.ngOnChanges (http://localhost:3004/vendor.dll.js:44196:89)
at Wrapper_FormControlDirective.ngDoCheck (/ReactiveFormsModule/FormControlDirective/wrapper.ngfactory.js:50:18)
答案 0 :(得分:31)
<form [formGroup]="form" (ngSubmit)="onSubmit(form.value)" class="form-horizontal">
<div class="form-group row">
<label for="inputEmail3" class="col-sm-4 ">Username</label>
<div class="col-sm-8">
<input formControlName="email" type="text" class="form-control" id="inputEmail3" placeholder="Email Address" [readonly]="isReadOnly">
</div>
</div>
</form>
请尝试将此更改[formControl]
改为formControlName
。
要将输出设置为输入字段,请执行以下操作,指向行this.form.patchValue
import { Component } from '@angular/core';
import { FormGroup, AbstractControl, FormBuilder, Validators } from '@angular/forms';
import { Router, ActivatedRoute } from '@angular/router';
import { EmailValidator, EqualPasswordsValidator } from '../../theme/validators';
@Component({
selector: 'register',
templateUrl: './register.html',
})
export class Register {
public form: FormGroup;
public email: AbstractControl;
public username: string;
constructor(private registerService: RegisterService, fb: FormBuilder, private router: Router, private route: ActivatedRoute) {
this.form = fb.group({
'email': ['', Validators.compose([Validators.required])]
.... etc..
});
this.email = this.form.controls['email'];
this.registerService.getUser({ userId: "asdasd2123da2das" }).subscribe(posts => {
if (posts) {
var userObj = posts.json();
console.log("userObj : ", userObj.data);
if (userObj.data && userObj.data[0].email) {
this.email = this.username = userObj.data[0].email; // ouput : abc@gmail.com
this.form.patchValue({
email : this.email
});
this.isReadOnly = true;
this.router.navigateByUrl('/register');
} else {
alert("You are Not Autorize to access this Page");
this.router.navigateByUrl('/login');
}
}
});
答案 1 :(得分:13)
我在标记中错过了formGroup中的方括号
<form formGroup="form">
</form>
,它会引发与
类似的错误错误TypeError:无法在字符串“ form”上创建属性“ validator”
一旦在formGroup中添加了方括号(见下文),错误就会消失
<form [formGroup]="form">....</form>
答案 2 :(得分:0)
有关每种形式的完整信息,请参见https://angular.io/guide/forms-overview网站上的反应性形式和模板驱动形式。您缺少将表单控件分组和在模板中注册控件的实际语法。两者在不同情况下均有效
<input type="text" [formControl]="name">
<input type="text" formControlName="firstName">