表单使用 google place api 获取地址,但点击地址(或使用键盘选择地址)不会触发表单的重新验证?
https://plnkr.co/edit/LgEn9t0X2h6BTHGwyJOW?p=preview
我使用更新值
this.registrationForm.patchValue({ adress: adress.formatted_address});
并尝试使用这些方法手动触发验证
this.registrationForm.markAsDirty(true);
和
this.registrationForm.updateValueAndValidity();
但无法让它工作。
最佳答案
由于 place_changed
事件在 Angular 区域外触发,我建议我们应该像这样调用 zone.run()
constructor(private zone: NgZone) {}
...
google.maps.event.addListener(this.autocomplete, 'place_changed', () => {
const place = this.autocomplete.getPlace();
this.zone.run(() => this.invokeEvent(place));
<强> Forked Plunker
另请参阅
关于forms - 选择地址时, Angular 形式不验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44189424/