是否有可能通过API将变量传递到formbuilder组中,以便将其包含在POST中到我的服务器?以下是我到目前为止所做的:
formAPICode: any;
ngOnInit() {
this.ionicForm = this.formBuilder.group({
apiKey: [this.formAPICode], // This is where the API Key should be appearing but doesnt. Console log returns NULL
name: ['', [Validators.required, Validators.minLength(2)]],
email: [
'',
[
Validators.required,
Validators.pattern('[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,3}$'),
],
],
mobile: ['', [Validators.required, Validators.pattern('^[0-9]+$')]],
street: ['', [Validators.required, Validators.minLength(2)]],
unit: [],
city: ['', [Validators.required, Validators.minLength(2)]],
state: ['', [Validators.required, Validators.minLength(2)]],
zip: ['', [Validators.required, Validators.minLength(5)]],
summary: ['', [Validators.required, Validators.minLength(2)]],
dateTime: [],
existingCustomer: [],
customerConsent: [Validators.required],
});
// Loads WP Company Data
this.wpService.postCompInfoDetails().subscribe((data) => {
this.companyDetail = data;
this.formAPICode = this.companyDetail.acf.forms_api_key;
console.log(this.formAPICode); // returns 45678451-1124d-45ae-bf15-ABC
});
}
下面是控制台JSON
form data {"apiKey":null,"name":"John Doe","email":"[email protected]","mobile":"1324567894","street":"1234 Main St","unit":"Suite 202","city":"New York","state":"NY","zip":"10001","summary":"Description of summary","dateTime":null,"existingCustomer":null}
如果我把formAPICode改成这样,它就可以正常工作了。当我通过外部API引入API密钥时,它不会
formAPICode = '45678451-1124d-45ae-bf15-ABC';
1条答案
按热度按时间1tu0hz3e1#
看起来好像是在表单控件获得任何值之前,您将
formAPICode
值赋给了它。然后,当您实际从API获取值时,您正在更新formAPICode
属性,但表单控件已经获得了初始值。试试这个: