如何访问 Angular Controller 中的 ons-carousel 变量?
How to access ons-carousel variable in Angular Controller?
我想在我的 AngularJS 控制器中将事件处理程序绑定到我的 ons-carousel postchange 事件,但是我一直收到未捕获的类型错误:无法读取未定义的 属性 'on' .
我尝试通过控制器的 var 属性访问轮播:
$scope.myCarousel.on('postchange', function() {
// do something
});
我的轮播在我看来是这样声明的:
<ons-carousel var="myCarousel" swipeable overscrollable auto-scroll>
...
</ons-carousel>
它有效,但是我无法访问变量 myCarousel 并且无法将事件绑定到它。我做错了什么?
我搜索了一下 ons-carousel 的文档,我可以通过在视图中向我的旋转木马添加 ons-postchange="changeCurrentPictureIndex()"
属性来解决这个问题,并且在我的 changeCurrentPictureIndex 方法中以某种方式我能够访问 myCarousel 变量。
在访问 Carousel 变量之前使用 ons.ready 函数。因为您只能在完成温泉初始化后访问控制器中的变量。
ons.ready(function() {
myCarousel.on('postchange', function(event) {
//your code
});
});
我想在我的 AngularJS 控制器中将事件处理程序绑定到我的 ons-carousel postchange 事件,但是我一直收到未捕获的类型错误:无法读取未定义的 属性 'on' .
我尝试通过控制器的 var 属性访问轮播:
$scope.myCarousel.on('postchange', function() {
// do something
});
我的轮播在我看来是这样声明的:
<ons-carousel var="myCarousel" swipeable overscrollable auto-scroll>
...
</ons-carousel>
它有效,但是我无法访问变量 myCarousel 并且无法将事件绑定到它。我做错了什么?
我搜索了一下 ons-carousel 的文档,我可以通过在视图中向我的旋转木马添加 ons-postchange="changeCurrentPictureIndex()"
属性来解决这个问题,并且在我的 changeCurrentPictureIndex 方法中以某种方式我能够访问 myCarousel 变量。
在访问 Carousel 变量之前使用 ons.ready 函数。因为您只能在完成温泉初始化后访问控制器中的变量。
ons.ready(function() {
myCarousel.on('postchange', function(event) {
//your code
});
});