0

I have been working around onsen-ui recently and got stuck when I was trying to reference a Carrousel var (var="carousel") within a Tabbar and Navigator components.

The code works until I try to use Tabs. I get the following error:

Uncaught (in promise) TypeError: Cannot read property of undefined

I have tired the ‘pageinit’ solution but it doesn’t seem to work.

Here under the HTML.

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta http-equiv="Content-Security-Policy" content="default-src * data:; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'">
    <link rel="stylesheet" href="lib/onsen/css/onsenui.css">
    <link rel="stylesheet" href="lib/onsen/css/onsen-css-components.css">
    <link rel="stylesheet" href="css/style.css">
    <script src="lib/onsen/js/angular/angular.min.js"></script>
    <script src="lib/onsen/js/onsenui.min.js"></script>
    <script src="lib/onsen/js/angular-onsenui.min.js"></script>
    <script src="js/app.js"></script>
</head>
<body ng-controller="AppCtrl">
    <ons-tabbar>
      <ons-tab page="home.html" active="true">
        <ons-icon icon="ion-home"></ons-icon>
        <span style="font-size: 14px">Home</span>
      </ons-tab>
    </ons-tabbar>
    <ons-template id="home.html">
        <ons-navigator title="Navigator" var="myNavigator">
            <ons-page>
              <ons-toolbar>
                <div class="center">Carousel</div>
              </ons-toolbar>
              <ons-carousel ng-controller="carouselCtrl" swipeable overscrollable auto-scroll fullscreen var="carousel">
                <ons-carousel-item style="background-color: gray;" id="carouselWho">
                  <div class="item-label">1</div>
                </ons-carousel-item>
                <ons-carousel-item style="background-color: #D38312;" id="carouselWhere">
                  <div class="item-label">2</div>
                </ons-carousel-item>
                <ons-carousel-cover>
                  <div class="cover-label">
                    <ons-icon icon="ion-ios-home" size="20px" fixed-width="true" ng-class="{'custom-icon': iconIndex == 'carouselWho'}"></ons-icon>
                    <ons-icon icon="ion-ios-location" size="20px" fixed-width="true" ng-class="{'custom-icon': iconIndex == 'carouselWhere'}"></ons-icon>
                </div>
                </ons-carousel-cover>
              </ons-carousel>
            </ons-page>
        </ons-navigator>
    </ons-template>
</body>
</html>

And app.js

var module = ons.bootstrap('my-app', ['onsen']);

module.controller('AppCtrl',['$scope', function($scope) {

}]);

module.controller('carouselCtrl',['$scope', function($scope) {

    ons.ready(function() {

        $scope.iconIndex = $scope.carousel._attrs.$$element[0].children[$scope.carousel.getActiveIndex()].id;
        $scope.$apply();

        $scope.carousel.on('postchange', function (event) {
            $scope.iconIndex = $scope.carousel._attrs.$$element[0].children[$scope.carousel.getActiveIndex()].id;
            $scope.$apply()
        });

   });

}]);

Do you have any idea where the issue comes from?

Many thanks for your time.

1 Answers1

0

I added a tab to your HTML and put it on codepen. It seems to work? Can you specify what happens when you get the error?

<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta http-equiv="Content-Security-Policy" content="default-src * data:; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'">
</head>
<body ng-controller="AppCtrl">
    <ons-tabbar>
      <ons-tab page="home.html" active="true">
        <ons-icon icon="ion-home"></ons-icon>
        <span style="font-size: 14px">Home</span>
      </ons-tab>
      <ons-tab page="home2.html" active="true">
        <ons-icon icon="ion-home"></ons-icon>
        <span style="font-size: 14px">Home</span>
      </ons-tab>
    </ons-tabbar>
    <ons-template id="home.html">
        <ons-navigator title="Navigator" var="myNavigator">
            <ons-page>
              <ons-toolbar>
                <div class="center">Carousel</div>
              </ons-toolbar>
              <ons-carousel ng-controller="carouselCtrl" swipeable overscrollable auto-scroll fullscreen var="carousel">
                <ons-carousel-item style="background-color: gray;" id="carouselWho">
                  <div class="item-label">1</div>
                </ons-carousel-item>
                <ons-carousel-item style="background-color: #D38312;" id="carouselWhere">
                  <div class="item-label">2</div>
                </ons-carousel-item>
                <ons-carousel-cover>
                  <div class="cover-label">
                    <ons-icon icon="ion-ios-home" size="20px" fixed-width="true" ng-class="{'custom-icon': iconIndex == 'carouselWho'}"></ons-icon>
                    <ons-icon icon="ion-ios-location" size="20px" fixed-width="true" ng-class="{'custom-icon': iconIndex == 'carouselWhere'}"></ons-icon>
                  </div>
                </ons-carousel-cover>
              </ons-carousel>
            </ons-page>
        </ons-navigator>
    </ons-template>
    <ons-template id="home2.html">
        <ons-navigator title="Navigator" var="myNavigator">
            <ons-page>
              <ons-toolbar>
                <div class="center">Carousel</div>
              </ons-toolbar>
              <ons-carousel ng-controller="carouselCtrl" swipeable overscrollable auto-scroll fullscreen var="carousel">
                <ons-carousel-item style="background-color: gray;" id="carouselWho">
                  <div class="item-label">3</div>
                </ons-carousel-item>
                <ons-carousel-item style="background-color: #D38312;" id="carouselWhere">
                  <div class="item-label">4</div>
                </ons-carousel-item>
                <ons-carousel-cover>
                  <div class="cover-label">
                    <ons-icon icon="ion-ios-home" size="20px" fixed-width="true" ng-class="{'custom-icon': iconIndex == 'carouselWho'}"></ons-icon>
                    <ons-icon icon="ion-ios-location" size="20px" fixed-width="true" ng-class="{'custom-icon': iconIndex == 'carouselWhere'}"></ons-icon>
                  </div>
                </ons-carousel-cover>
              </ons-carousel>
            </ons-page>
        </ons-navigator>
    </ons-template>
</body>
</html>

https://codepen.io/grailly/pen/wWLavb?&editors=101

Gary
  • 3
  • 3
  • Hello, thanks for answering. I believe I was not clear, sorry. My problem is not related to the tab itself but with reading the 'carousel' variable at initialization when inside a tab. – Charly Nedsson Aug 26 '16 at 12:35