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

Filtering data in AngularJS by parsing JSON records

I have a JSON file containing restaurant information and I need to display the data by grouping them based on their respective address fields. For example, all restaurants with the address 'Delhi' should be shown first, followed by those from &ap ...

Using JavaScript, retrieve the ID of a child element by utilizing details about the parent element

I have implemented a JavaScript function that creates a div element. Within this div, there are multiple checkboxes as child elements. My goal is to use a loop to extract the ids of all these checkboxes. The div has been assigned to a variable named: o ...

Identify when two calendar dates have been modified

Creating a financial report requires the user to select two dates, search_date1 and search_date2, in order for a monthly report to be generated. Initially, I developed a daily report with only one calendar, where I successfully implemented an AJAX script ...

What is preventing NgClass from applying the CSS styles?

I'm currently facing an issue in Angular2 where I am trying to apply different styles using ngClass within an 'NgFor' loop, but for some reason, it's not working as expected. Apologies for any language errors. <div class='line ...

Turning a stateful React component into a stateless functional component: Ways to achieve functionality similar to "componentDidMount"

I recently developed a small, stateful React component that utilizes Kendo UI to display its content in a popup window when it loads. Here is a snippet of the code: export class ErrorDialog extends React.Component { constructor(props, context) { sup ...

Tips for accessing basic information from these websites without encountering CORS issues

Having issues retrieving data from the following two sites: and Eurolottery. The CORS issue is causing the problem, and I was able to retrieve data using a Chrome CORS extension and the provided code below: var HttpClient = function() { this.get = fu ...

It appears that Next.js's useDebouncedCallback function is not effectively delaying the request

I am currently learning Next.js and trying to work through the tutorial. I have hit a roadblock on this particular page: https://nextjs.org/learn/dashboard-app/adding-search-and-pagination Despite conducting an extensive web search, I couldn't find a ...

My jQuery form is not functioning properly upon initialization

Let's take a look at this sample 'template' code: $(document).on("<EVENT>", "form", function() { $(this).find(".input input").each(function() { var required = $(this).attr("required"); var checkField = $(this).clos ...

Tips for adding a picture to a server and applying CSS filters to enhance it

I recently came across a set of CSS filters that can be applied to images by simply specifying the filter ID. Now, I'm looking to create a button that will allow me to save the edited image (with the filter applied) to a designated file location. I&a ...

What is the best way to add table pagination at the bottom of a table?

Can someone provide guidance on implementing table pagination for the material-ui table below? The documentation is a bit unclear: <Table ria-label="a dense table"> <TableHead> <TableRow> ...

"Unraveling the Mystery: In Javascript Vue, what is the secret behind the origin of the variable 'e' found in the function

Where does the mysterious variable e in onFileChange(e) come from? In the code snippet below, we see a variable e being used in onFileChange(e). However, this variable is not declared or imported anywhere in the code. How is it possible for this to be val ...

Improper menu alignment following a MenuItem hover event within Material-UI

When a MenuItem is hovered over, the position of the Menu container should remain unchanged. [x] The issue persists in the most recent release. However, downgrading MUI to v4.5.0 results in the expected behavior. Current Behavior ...

Creating a shadow effect within an element: a step-by-step guide

I've been experimenting with creating an inner shadow effect on an image, but I can only seem to achieve an outer shadow. Just for clarification, .showSlide is a div element. .showSlide { display: block; width:100%; height:350px; } .showSlide img { ...

How can the 'selected' attribute be assigned to 'select/option' tags depending on the option value?

In my code, I have a select input with various options and values. I want to set the 'selected' attribute for the option that corresponds to a specific value X. Here is an example: // If x=3, I want the option with value=3 to be marked as 's ...

What could be causing my dropdown menu to not appear when clicked?

I am trying to implement the functionality shown in the image. When the Settings button is clicked, a window should open allowing the user to navigate to their desired option. However, I am facing an issue where nothing happens when the button is clicked. ...

Stringification will not work on the virtual object that has been populated

Here is the object passed to the view: app.get('/view_add_requests', isLoggedIn, function (req, res) { var my_id = req.user._id; // this is the senders id & id of logged in user FriendReq.find({to_id: my_id}).populate('prof ...

Tips on formatting text as bold or italic when tweeting from my website to our Twitter account

Currently, I am utilizing the Twitter OAuth library in conjunction with PHP to publish a tweet from my website directly to my Twitter account. My main objective is to highlight some text while posting, however, I have not been successful in identifying t ...

Developing a TypeScript NodeJS module

I've been working on creating a Node module using TypeScript, and here is my progress so far: MysqlMapper.ts export class MysqlMapper{ private _config: Mysql.IConnectionConfig; private openConnection(): Mysql.IConnection{ ... } ...

Assign an array value to the input based on the selection made in Javascript

After finding a lot of useful information in previous questions, I am struggling to get the last piece that I need. My PHP code helps me loop through form fields and assign unique names to each field using a variable. One of these fields is for "expense ty ...

There seems to be an issue with the $scope value not being properly updated

In my AngularJS application, I am trying to update a $scope variable and display it in a Bootstrap modal. However, the new value is not appearing in the modal. Here is my code: View: <li ng-controller="QuickViewController"> <a href="javascri ...