开发者社区> 问答> 正文

重定向到具有查询参数的URL并将值分配给查询参数中的反应形式不起作用怎么办-Angular?

我正在重定向到带有查询参数的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: [''],
});

}

展开
收起
被纵养的懒猫 2019-09-17 16:45:02 626 0
0 条回答
写回答
取消 提交回答
问答分类:
API
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载