Adjust the active carousel item to 0 within onsen-ui (displaying a list of carousel items in a sliding menu)

In my sliding menu, each menu item contains a carousel with two items. I am trying to make the first carousel item show after closing and reopening the menu, or by clicking a button outside of the list on the menu page.

This is my current setup:

<ons-template id="menu.html">
         <ons-page>
            <span style = "text-align:center;width:30dp;margin:25%;">Presentations</span>
            <ons-list id ="list"  style="padding:0px;" ng-controller="ItemController">

            <ons-list-item ng-repeat="item in local" style="padding:0;">
              <ons-carousel  swipeable auto-scroll auto-refresh initial-index="0" style="height: 100%;width: 100%; position: absolute;">
                <ons-carousel-item style="padding:0;">
                  <ons-button modifier = "quiet" ng-click="menu.close();customSetMainPage(item.id);">
                    {{item.name}}
                  </ons-button>
                </ons-carousel-item>
                <ons-carousel-item style ="text-align: center;">
                  <ons-button ng-click="deletepresentation(item.id);local.splice($index, 1);" >
                        Remove
                        <ons-icon icon="ion-trash-a"/>
                  </ons-button>
                </ons-carousel-item>
              </ons-carousel>
            </ons-list-item>
          </ons-list>

            <p>
               <ons-button modifier = "small" style="margin-left: 25%;"  id = "add"
                  onclick="
                  addPresentation();
                  menu.closeMenu();
                  if (runtime.diaginit == false) {
                  runtime.diaginit=true;
                  ons.createDialog('dialog.html').then(function(dialog) {dialog.show();});}
                  else {dialog.show();}
                  "
                  >
                  new  presentation 
               </ons-button>
            </p>
         </ons-page>
      </ons-template>

EDIT :

I attempted to proceed as follows: This line

angular.element(document.getElementById('list')).scope().itemToDefault();
causes an error in my app, displaying:

undefined is not a function (evaluating '$scope.carousel[index].first()')

ons.ready(function() {
           menu.setMainPage('first.html');
           menu.on('postclose', function() {
            ons.compile(document.getElementById("carousel"));

          });
           menu.on('preclose', function() {
                angular.element(document.getElementById('list')).scope().itemToDefault();

          });
      });


var application = angular.module('app', ['onsen']);
application.controller('ItemController',function($scope){
    $scope.local = [];
    $scope.itemToDefault = function (){
            var c = $scope.carousel;
            for (var index in $scope.carousel) {
                if( $scope.carousel.hasOwnProperty( index ) ) {
                     $scope.carousel[index].first(); 
                } 
            }
        };

    });

Answer №1

If you want to access your carousels, make sure to assign a "name" to them like this:

<ons-carousel var="carousel.{{$index}}" ...>
. Then use the following code snippet to reset each carousel back to its initial index:

for (var index in $scope.carousel) {
  if( $scope.carousel.hasOwnProperty( index ) ) {
    $scope.carousel[index].first();
  } 
}

You can call this code whenever needed, whether before or after a button action in the menu, or on events like postclose or preopen of the Sliding Menu (

mySlidingMenu.on('postclose', restore_all_carousels)
).

I hope this information proves useful!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

While utilizing Yeoman build, an error occurs: "Uncaught Error: Unknown provider: aProvider <- a"

After running the yeoman build command and trying to access my web app, I encountered the following error An Uncaught Error occurred: Unknown provider: aProvider <- a ...

Unable to assign value to a public variable in Angular

I am facing an issue where I am trying to retrieve a value from the localStorage and assign it to a variable. However, when I try to use that variable in functions, it is coming up as undefined. code export class DashboardService { public token: any; ...

Create a duplicate array with all distinct elements - JavaScript

Help needed: I am facing an issue with an array of objects. My goal is to create a unique copy of this array to prevent any changes in the original one when modifications are made to the new array. I have experimented with several methods, including: let ...

Login and session management for users using AngularJS in the client-side and Spring in the backend

Currently, my mobile website is built using angularjs, and the backend is powered by Spring Boot. The issue I am facing is that when a user logs in successfully, their information is obtained from the server, but if they click "back" or "refresh" on the br ...

Developing desktop applications with Angular 2 using NWjs

Looking to create an Angular 2 Desktop App using NWjs. Where can I find the entry point? Could someone provide some examples of developing Angular 2 Desktop Apps with NW.js? ...

Learn the steps for assigning a distribution tag to an npm package within a private registry

Operating with my own exclusive Gemfury repository, I am actively releasing npm packages. Intrigued by the prospect of applying distribution tags to my packages (as per this guide: https://docs.npmjs.com/cli/dist-tag). The configuration of my npm registr ...

Can a single controller function in ASP.NET MVC4 be used to send back two arrays to the view?

Continuing from my previous query here, I am working with a model called SchoolTestsPerModulePerStudent which looks like this: public partial class SchoolTestsPerModulePerStudent { public long StudentID { get; set; } public long ModuleID { get; se ...

The object model being utilized in Angular Formly remains static even after I attempt to reassign it

Using formly, I am able to modify objects retrieved from a server. After making the necessary edits, I save the changes. However, when the server sends back the updated object, I update my model object in formly with this new data. Strangely though, formly ...

What are the steps to utilize vue.js for dynamically adjusting my sidebar based on a URL input?

Greetings to the amazing community of Vue.js enthusiasts, I am a novice looking to develop a single-page web application using vue.js. This application will consist of a fixed header and dynamic body content that changes based on user interactions. Here&a ...

The $broadcast method in AngularJS allows us to send messages to

Is it possible to determine the outcome of $broadcast in AngularJS - whether it was successful or not? $rootScope.$broadcast($scope.abc + ':' + type + 'question', { abcSlug: $scope.abcSlug, questionSlug: questionSlug, abcOb ...

Angular error: Attempting to reduce an empty array without an initial value

I am encountering an issue with my array being filtered and reduced. getPageComponents(title: string) { this.pageComponents = []; const pageBlock = this.pageComponents.filter((val) => { if (val.page_title === title) { retur ...

Receiving server data with socket.io in Node.js

I have set up a Node.js server to send data to an HTML client using socket.io: var spawn = require('child_process').spawn; var child = spawn('node', ['file.js']); child.stdin.write("Hello there!"); child.stdout.on(&apo ...

When the click event is triggered, the second function guess() does not execute

I am currently developing a number guessing application. It consists of two functions, the first one called startGame() works correctly (it receives the maximum number and then disappears by adding the hidden class). However, the second function that is ...

How can you enhance functionality in Polymer without the need to create a custom element?

This is the code snippet from my index.html file in Polymer 1.1: <template is="dom-bind" id="app"> <paper-drawer-panel force-narrow id="the-drawer"> <paper-header-panel drawer> <paper-toolbar></paper-too ...

Exploring the combination of Tailwind CSS variants with Framer Motion capabilities

Is it possible to integrate tailwind css classes with framer motion objects effectively? const variant = { hidden: { 'w-0' }, visible: { width: 400, transition: { type: 'spring', stiffness: ...

In relation to the Uncaught Error: Syntax error, an unrecognized expression has been encountered

Recently, I started working on an AngularJS and Node.js application. It's all new to me. In the HTML page, I defined a link as <li><a data-toggle="modal" data-target="#myModal" href="/#/login">Login</a></li>, and then set up th ...

Error encountered with Vuetify stepper simple component wrapper and v-on="$listeners" attribute

I'm working on developing a custom wrapper for the Vuetify Stepper component with the intention of applying some personalized CSS styles to it. My aim is to transmit all the $attrs, $listeners, and $slots. I do not wish to alter any functionality or ...

CSS Flexibility in Action

Presently, my tab bar has a fixed look as shown here: https://codepen.io/cdemez/pen/WNrQpWp Including properties like width: 400px; etc... Upon inspecting the code, you'll notice that all the dimensions are static :-( Consequently, I am encountering ...

CSS not reflecting changes after the href of the <link> tag has been updated

Hey there, I'm a beginner in the world of programming and currently facing an issue that I need help with. My goal is to dynamically update the CSS of my webpage by using JQuery to change the 'href' value in the link tag. In order to test t ...

Parsing XML to JSON using JavaScript

I came across this JavaScript function on Stack Overflow that I've been using to convert XML to JSON: function xmlToJson(xml) { try { var obj = {}; if (xml.nodeType == 1) { if (xml.attributes.length > 0) { ...