angular - Ionic 5 手势不适用于 Angular 绑定
问题描述
如文档中所述,我有一个使用 GestureController 进行简单滑动手势的 Ionic 页面。在滑动结束事件中,我增加一个计数器。页面的 html 上有一个绑定,它没有得到更新。但是,Console.log() 会显示更新后的计数器。
是否存在已知问题或者我做错了什么?
页面模板
<ion-content #contentElement>
{{ counter }}
</ion-content>
打字稿
export class TestPage {
counter = 0;
swipeGesture: Gesture;
@ViewChild('contentElement', { static: true, read: ElementRef }) contentElement: ElementRef;
constructor(private gestureController: GestureController) { }
ionViewDidEnter() {
this.swipeGesture = this.gestureController.create({
el: this.contentElement.nativeElement,
gestureName: 'swipe',
onEnd: () => this.onSwipeEnd(),
});
this.swipeGesture.enable();
}
private onSwipeEnd() {
this.counter++;
console.log(this.counter);
}
}
解决方案
我有一个类似的问题,我所做的就是将它注入ChangeDetectorRef
到构造函数中。
constructor(private ref: ChangeDetectorRef) {}
然后,您可以手动运行更改检测。
private onSwipeEnd() {
this.counter++;
console.log(this.counter);
this.ref.detectChanges(); // Runs the change detection
}
推荐阅读
- angular - 当我们单击 kendo-multiselect 中的标签时,未选中复选框
- node.js - 用于比较和提取值的 MongoDB ID 数组
- reactjs - 使用 Strapi 和 React.js 发送邮件
- ios - PushKit 通知在通话结束后到达
- javascript - 从变量引用时,setAttribute 不是函数
- javascript - scope.data 没有 foreach 方法
- python - 如果它包含指定的短语,则删除整个 JSON 对象(来自 python 中的列表)
- php - 没有这样的文件或目录:我该如何解决这个问题?
- javascript - Firebase 云功能在部署时未更新 - 仍在服务旧版本的功能
- javascript - jQuery 函数仅在调试器模式下工作