jQuery Swiper script to run after Ng-Repeat elements are loaded

后端 未结 3 1335
甜味超标
甜味超标 2021-02-19 04:36

I\'m making a web app using AngularJS, jQuery, HTML, CSS and Bootstrap, and I would like to pick some image links from my JSON that is located in an Apache2 server and use them

3条回答
  •  北海茫月
    2021-02-19 05:27

    I'm answering to this old question because some guys asked me to do it and because I want to help other developers with this "funny" problem.

    After more than one year, I managed to solve this issue using Promises and Factories.

    Essentially, I made a Factory, ItemFactory ensuring to get all the items needed (in this case slides), then, I made another Factory, SwiperFactory, which uses a Promise to trigger that funny guy, swipers(). I called all those factory methods into a MainController, which, after getting the slides and triggering the script, does $scope.apply(); to reflect the changes in the main view.

    Main.html:

    //MainController known as mainCtrl
    

    MainController.js:

    angular
    .module('myApp')
    .controller('MainController', MainController);
    
    function MainController($scope, ItemFactory, SwiperFactory) {
    
       var vm    = this;
       //Initializing slides as an empty array
       vm.slides = [];
    
       /** Gets called as soon as the Controller is called **/
       getData('main.json');
    
       function getData(path){
          ItemFactory.getItems(path).then( function(response){
    
            //If response is not empty
            if (response) {
               //Assigning fetched items to vm.items
               vm.slides = response.data;
            }
    
            //Calling triggerSwiper after we ensured fetching item data
            SwiperFactory.triggerSwiper();
    
            //Callig $scope.$apply() to reflect correctly the changes in the view
            $scope.$apply();
          })
       };
    
    }
    

    ItemFactory.js:

    angular
    .module('myApp')
    .factory('ItemFactory', function ($http) {
    
        /** Using this referring as ItemFactory Controller **/
        var vm     = this;
        vm.API_URL = 'http://localhost/';
    
        /** Gets items from API based on jsonPath **/
        function getItems(jsonPath) {
            return $http.get(vm.API_URL + jsonPath
            ).then(function (response) {
                return (response);
            });
        }
    
        //Exposing getItems method
        return {
            getItems : getItems
        }
    });
    

    SwiperFactory.js:

    angular
    .module('myApp')
    .factory('SwiperFactory', function () {
    
        /** Using this referring as SwiperFactory Controller **/
        var vm     = this;
    
        /** Triggers Swiper Script 
         *  Resolving only after swiper has been triggered **/
        function triggerSwiper(){
          return new Promise( function(resolve, reject){
            resolve({
                $(document).ready(function (){
                    var swiper = new Swiper('.swiper-container',{
                    direction           : 'horizontal',
                    pagination          : '.swiper-pagination',
                    paginationClickable : true
                })
            });
    
          }, function(error){
             console.log('Error while triggering Swiper');
          })
    
        }
    
        //Exposing triggerSwiper method
        return {
            triggerSwiper : triggerSwiper
        }
    });
    
    • $scope.$apply(); can be removed, it is not really essential
    • In general, this kind of approach works well with asynchronous tasks

    The code above does the work, but, please, pay attention to what I'm saying below

    Try avoiding using jQuery libraries such as iDangero.us Swiper in an AngularJS project. Instead, look for an Angular library which makes the same work. If back then I was skilled like I'm right now, I would have never used it, expecially for the $(document).ready stuff. Instead, study Promises or AngularJS way of doing that. I was totally new to AngularJS and Web Development in general, so I took wrong decisions.

    I hope I've been helpful.

提交回复
热议问题