[英]Angular Observable doesn't update in view after calling .next()
I am using Google Maps API to validate an address.我正在使用 Google Maps API 来验证地址。 I have a service to handle this in a function called checkDistance() In the service, I created an observable called verifyMessage$ to communicate in the view if the address is valid or not.
我有一个服务来在一个名为checkDistance()的函数中处理这个在该服务中,我创建了一个名为 verifyMessage$ 的可观察对象,以便在地址有效与否时在视图中进行通信。
The view contains a verify button that calls the component function verifyAddress() which then calls the service's checkDistance();该视图包含一个验证按钮,该按钮调用组件函数verifyAddress() ,然后调用服务的checkDistance();
Here's the problem: the observable is not updated with the new value until I click the page again.问题是:在我再次单击页面之前,observable 不会更新为新值。 I click verify, and then in the console I can see the result has been set but the "Loading..." does not go away and the verifyMessage value is not updated until I randomly click the page somewhere.
我单击验证,然后在控制台中我可以看到结果已设置,但“正在加载...”不会消失并且 verifyMessage 值不会更新,直到我在某处随机单击页面。 What am I doing wrong?
我究竟做错了什么? How do I fix this?
我该如何解决?
delivery-component.html交付-component.html
<input formControlName="address" matInput placeholder="Start typing delivery address..." google-place>
<button mat-raised-button color="primary" (click)="verifyAddress()" class="col-2">Verify</button>
<p class="col-12" *ngIf="checkout.verifyMessage$ | async as message; else no message">{{ message.value }}</p>
<p class="col-12" *ngIf="checkout.loading">Loading...</p>
checkout service:结账服务:
...
async checkDistance(location_obj) {
this.loading = true;
... Google Maps API stuff ..
this.result = this.checkValidity(distance);
console.log('result is: ', this.result);
if (this.result) {
// if valid set this message so can be updated in view
this.verifyMessage$.next({value: 'Great, that address is in our range.'});
this.loading = false;
this.verified=true;
} else {
this.verifyMessage$.next({value: 'Uh oh, that address is out of our range. '});
this.loading = false;
this.verified=false;
}
});
}
deliveryPrompt.component.ts DeliveryPrompt.component.ts
import ...
export class DeliveryPromptComponent implements OnInit {
deliveryAddressForm: FormGroup;
message: string;
verified$ = new BehaviorSubject({value: false});
verified = this.verified$.asObservable();
loading = false;
constructor(public dialogRef: MatDialogRef<DeliveryPromptComponent>, @Inject(MAT_DIALOG_DATA) public data: any, public checkout: CheckoutHandlerService, public fb: FormBuilder, public loginService: AuthService, private cd: ChangeDetectorRef) {
}
ngOnInit() {
this.checkout.verifyMessage$.subscribe();
this.checkout.verifyMessage.subscribe();
this.deliveryAddressForm = this.fb.group({
address: ['', [Validators.required]]
});
this.deliveryAddressForm.valueChanges.subscribe(form => {
//console.log(form);
});
this.message = '';
}
...
async verifyAddress() {
console.group('loading 1', this.loading);
await this.checkout.checkDistance(this.checkout.tempAddress);
}
i had a similar problem using angular +8.我在使用 angular +8 时遇到了类似的问题。 When i use only
当我只使用
ng serve
angular rxjs was not sending the next value, but if i use angular rxjs 没有发送下一个值,但如果我使用
ng serve --aot=true
it works, it should be an issue on optimization.它有效,应该是优化问题。 Hope it helps!
希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.