I'm reading through this tutorial on integrating stripe elements with Angular我很好奇为什么 onChange
方法最后会调用 detectChanges()
。 onChange
方法作为事件监听器添加到 strip 卡,如果 onChange
接收到一个错误,它将分配一个错误。
onChange({ error }) {
if (error) {
this.error = error.message;
} else {
this.error = null;
}
this.cd.detectChanges();
}
最佳答案
我猜这与 Angular Change Detection Strategy 有关。
每次在组件中传播事件(鼠标、单击、输入、XHR 等)时,默认策略都会检查 View 。此策略不适用于复杂的应用程序,因为性能可能很糟糕。
另一种方法是使用OnPush
策略。仅当 @Input
已更改或您明确要求使用 ChangeDetectorRef.detectChanges();
关于javascript - 为什么 Angular Stripe 集成需要 ChangeDetectorRef.detectChanges()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57343256/