我正在重定向到带有查询参数的URL,并使用setValue将查询参数中的值分配给表单,但最初不显示值,而是在刷新后显示。
从查询参数获取值并分配给表单:
constructor(private fromBuilder: FormBuilder, private _API: RestService, private storageService: StorageService, private route: ActivatedRoute) {
if (!this.formsInitialized) this.initForms()
this.route.queryParams.subscribe((params) => {
if (params.user) {
this.registerForm['controls']['cpName'].setValue(JSON.parse(decodeURIComponent(params.user)).name, { onlySelf: false })
this.registerForm['controls']['cpEmail'].setValue(JSON.parse(decodeURIComponent(params.user)).email, { onlySelf: false })
}
})
}
ngOnInit() { }
initForms() {
this.formsInitialized = true
this.registerForm = this.fromBuilder.group({
cpName: ['dfdfd', [Validators.required, Validators.minLength(4), Validators.maxLength(30), Validators.pattern(this.namePattern)]],
cpEmail: ['', [Validators.required, Validators.pattern(this.emailPattern)]],
dname: ['', [Validators.required, Validators.maxLength(15), Validators.pattern(this.domainPattern)]],
bname: ['', [Validators.required, Validators.maxLength(50), Validators.pattern(this.brandPattern)]],
password: ['', [Validators.required, Validators.minLength(6), Validators.maxLength(15)]],
token: [''],
googleId: [''],
imageUrl: [''],
});
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。