如何访问Angular Controller中的旋转木马变量

How to access ons-carousel variable in Angular Controller?

本文关键字:Controller 旋转木马 变量 Angular 何访问 访问      更新时间:2023-09-26

我想在我的AngularJS控制器中将事件处理程序绑定到我的ons carousel postchange事件,但我一直得到一个Unaught TypeError:无法读取未定义的属性'on'。

我试图通过我的控制器的var属性访问我的旋转木马:

$scope.myCarousel.on('postchange', function() {
  // do something
});

我的旋转木马在我看来是:

<ons-carousel var="myCarousel" swipeable overscrollable auto-scroll>
...
</ons-carousel>

它是有效的,但是我不能访问变量myCarousel,也不能将事件绑定到它。我做错了什么?

我搜索了一点ons carousel的文档,我可以通过在视图中向我的carousel添加ons-postchange="changeCurrentPictureIndex()"属性来解决这个问题,并且在我的changeCurrentPictureIndex方法中,我能够访问myCarousel变量。

在访问Carousel变量之前使用ons.ready函数。因为您只能在完成onsen初始化后访问控制器中的变量。

ons.ready(function() {
    myCarousel.on('postchange', function(event) {
        //your code
    });
});