Issue with resizing Ionic carousel when making an $http request

In my Ionic project, I am utilizing a plugin to create a carousel (https://github.com/ksachdeva/angular-swiper). The demo of this plugin includes a simple repeat function. However, when I replaced the default repeat with my own using $http, it caused an issue where there is a delay in loading the images, leading to the slider breaking until it is resized. Here is how the HTML structure looks:

<ks-swiper-container autoplay="500" initial-slide="1" speed="5000" loop="false" show-nav-buttons="false" slides-per-view="2" space-between="20" pagination-clickable="false" override-parameters="{effect: 'coverflow',coverflow: {rotate: 0,stretch: 0,depth: 100,modifier: 1,centeredSlides: true,slideShadows : false}}" on-ready="onReadySwiper(swiper)">

            <ks-swiper-slide class="swiper-slide" ng-repeat="feature in featured track by feature.id">
                <img imageonload="" ng-src="{{feature.thumbnail_images.thumbnail.url}}" width="100%">
                <h6 ng-bind-html="feature.title"></h6>
            </ks-swiper-slide>

           <ks-swiper-slide class="swiper-slide">
            <img src="img/more.png" style="transform: rotate(180deg);" width="100%">
            <h6>Read More</h6>
            </ks-swiper-slide>

        </ks-swiper-container> 

The images are fetched from my factory using the following approach:

.controller('SwipeCtrl', function($scope, Featured) {

    $scope.swiper = {};
    $scope.onReadySwiper = function (swiper) {

        swiper.on('slideChangeStart', function () {
        });

        swiper.on('onSlideChangeEnd', function () {
        });   

    };




  $scope.featured = [];
  Featured.all().then(function (response,swiper){

      $scope.featured = response;
      for (var i =0; i < $scope.featured.length; i++) {
            $scope.featured[i].date = new Date($scope.featured[i].date);

        }
  }, 

  function (err) {
     if(window.localStorage.getItem("featured") !== undefined) {

      }
    }
  );
})

I attempted to use $timeout without success. After some research, I came across a suggestion to create a directive:

.directive('imageonload', function ($rootScope, $timeout) {
    return {
    restrict: 'A',
        link: function(scope, element, attrs) {

            element.bind('load', function() {
                $timeout(function(){
                    $rootScope.swiper.updateSlidesSize();
                });
            });


        }
    };
});

Despite implementing the directive, I consistently encounter the error "updateSlidesSize() is undefined". At this point, I'm uncertain about how to resolve this issue...

Answer №1

I encountered a similar issue recently. Upon closer inspection, I realized that the version of swiper.js I was using (not angular-swiper.js) did not contain the updateSlidesSize() function. After updating to a newer version of swiper.js, this function was included.

With that resolved, here is how I brought everything together:

//HTML snippet
<ks-swiper-container initial-slide="1" loop="false" show-nav-buttons="false" slides-per-view="3" space-between="5" pagination-clickable="false" on-ready="onReadySwiper(swiper)">...</ks-swiper-container>

//Controller logic
$scope.swiper = {}; //initialize
$scope.onReadySwiper = function (swiper)
{
  $scope.swiper = swiper; //update swiper when it's ready
};
$scope.myData=[1,2,3,4,5];
$http.jsonp(myurl+'&callback=JSON_CALLBACK').success(function(data)
{
   $scope.myData=data; //e.g.: [6,7,8]
   $timeout(function()
   {
     $scope.swiper.updateSlidesSize(); //update slide size
     $scope.swiper.slideTo(0); //go to first slide
   }, 300);
});

Please note that I have only tested this solution in Google Chrome with emulation, not on an actual mobile device.

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

Leveraging oEmbed - JSON data (within client-side JavaScript)

Is it feasible to utilize an oembed provider that solely produces json and xml outputs on the client side using javascript (through a $.ajax request to the provider)? Do we need to rely on oembed providers that enable a jsonp callback in javascript for th ...

"Adding a Dynamic Template to Your AngularJS Directive: A Step-by-Step

I am looking to develop a directive similar to the following: <div ng-refreshing-when="x">Some Content In Here</div> When x is true, I want it to apply specific CSS styles and show a spinner div on top of the content. Then when x is false, I ...

What is the process for transferring information from a Microsoft Teams personal tab to a Microsoft Teams bot?

Is it feasible to share data such as strings or JSON objects from custom tab browsers to a Teams bot's conversation without utilizing the Graph API by leveraging any SDK functionality? ...

Guide on sending an AJAX request for file upload to a Spring MVC controller

Hello, I am looking for assistance with setting up an AJAX call to send a file upload request to the controller in JavaScript. I have a dialog box where I am uploading a file, and I want to trigger the AJAX call upon clicking the upload button. If anyone h ...

The process of transferring ViewBag value as a parameter in an AngularJS function

I'm facing an issue where the viewbag value is not being passed as a parameter in ng-init. Can someone guide me on how I can successfully pass the viewbag value as a parameter? angular.js { $scope.detail = function (Id) { ...

Dynamic management of Spring Boot MongoDB Repositories across numerous databases

I was faced with the task of updating my Spring-Boot application to handle dynamically switching between multiple instances of the Mongo driver. The app already has Spring Boot MongoDB Repositories configured, but now we need to implement sass (dynamical ...

Parallel ajax function

After creating a form for registering new accounts, I wanted to ensure that the chosen email is available by checking it against a webservice. However, this process takes a few seconds. Let's take a look at the method used: function validateEmail(ema ...

Where should Babel and Webpack be placed in your package.json file - under devDependencies or Dependencies?

I'm a beginner when it comes to npm and I have doubts on what should be included in dependencies as opposed to devDependencies. I understand that testing libraries belong in dev, but what about tools like babel and webpack? Should they also be categor ...

What is the best way to access data from outside a forEach loop in JavaScript?

I am trying to access the value of my uid outside of the foreach loop in my code. Can anyone assist me with this? This is the code I am working with: var conf_url = "https://192.168.236.33/confbridge_participants/conference_participants.json?cid=009000 ...

Utilize jQuery script on every single element

Is there a way to implement a jquery function on elements that are dynamically loaded via ajax? <span class="h">Test</span><br /><br /> <span class="h">Test</span><br /><br /> <span class="h">Test</ ...

Intercepting HTTP responses to handle headers

I'm facing an issue where I am trying to retrieve a custom header sent from the server in my $http response interceptor. However, the only header that is accessible is the Content-type header. How can I troubleshoot this problem? Here is part of my $ ...

Attempting to output numerical values using Jquery, however instead of integer values, I am met with [Object object]

I am struggling to figure out how to display the value contained in my object after attempting to create a Calendar using Jquery. I attempted to use JSON.toString() on my table data, but it didn't solve the issue. Perhaps I am not placing the toString ...

Executing a Jquery click event after a delay with setTimeout

I am working with an <a> element that, when clicked, triggers a handler like this: function onCummReportClick(e) { if ($(e.currentTarget).attr('href').indexOf('csv') !== -1) { { return true; } //Here s ...

Converting PHP array to a JavaScript object

I am facing a scenario like this: Having JSON data: [{ "1377412272": { "user_id": "1374050643", "date": "2013-08-24", "ip": "::1" } }, { "1377412279": { "user_id": "137405064 ...

personalizing jquery templates

Currently, I am utilizing a jQuery template to showcase specific values. Within this template, I have included an if statement to determine if the age is considered old or not, with the result altering the background color accordingly. Previously, the co ...

Mastering HTML5 Video: A guide to smoothly playing multiple videos in a loop fashion

Although similar questions have been raised in the past, none has successfully addressed this particular issue; they only provide partial solutions. The objective is as follows: We possess a collection of videos referred to as video1, video2, video3, vid ...

Centering loaders within elements of an unordered list

I am working on a navbar that uses Bootstrap 3 and AngularJS. Within this navbar, I have an unordered list with li elements. Below is a snippet of my navbar: https://i.stack.imgur.com/o75Lp.png This is the code for my navbar: <div class="navbar-head ...

Ways to prevent modal from flickering during event changes

I'm struggling with a current issue and need help identifying the cause and finding a solution. The problem arises from having a nested array of Questions, where I display a Modal onClick to show Sub questions. However, when clicking on the Sub Quest ...

Retrieve information from an XML document

I have some XML content that looks like this: <Artificial name="Artifical name"> <Machine> <MachineEnvironment uri="environment" /> </Machine> <Mobile>taken phone, test when r1 100m ...

Customizing Material UI Themes

Is there a way to customize the MuiTheme overrides for a specific named element like this? .MuiStepLabel-label.MuiStepLabel-active { color: rgba(0, 0, 0, 0.87); font-weight: 500; I've attempted modifying various classes such as MuiStepLabelLa ...