Below I will share with you an article that solves the problem of not being able to slide when using the Swiper plug-in in Angular.js. It has a good reference value and I hope it will be helpful to everyone.
We all know that swiper is a carousel plug-in with a very good interactive experience
But the solution for swiper looped out through angular (ng-repeat) cannot be carousel
Usually we perform it through the following methods:
html
<p class="swiper-container" ng-controller="swiperController"> <p class="swiper-wrapper"> <p class="swiper-slide" ng-repeat="informarion in imgSrcs"> <img ng-src="{{informarion.sliderSrc}}" /> </p> </p> <!-- Add Pagination --> <p class="swiper-pagination"></p> <!-- Add Arrows --> </p>
js
var myapp=angular.module("myApp",[]); //轮播图的控制器 myapp.controller("swiperController",function($scope,$http){ //请求轮播图路径 $http({ method: 'post', url: 'json/myJson.json' }).then(function successCallback(response) { $scope.imgSrcs = response.data.sites; }, function errorCallback(response) { // 请求失败执行代码 }); });
But it still doesn’t work. Note: If you are getting data in json, you need to write the carousel js code in getting the data, because it gets the data first. If you place the carousel externally, the carousel data cannot be obtained.
So the solution is to put the initialization method of swiper into the $http request and execute it.
Add the following code to the function successCallback() method to achieve carousel
var swiper = new Swiper('.swiper-container', {//重置轮播加载方法 pagination: '.swiper-pagination', slidesPerView: 1, paginationClickable: true, spaceBetween: 30, keyboardControl: true, nextButton: '.swiper-button-next', prevButton: '.swiper-button-prev', observer:true,//修改swiper自己或子元素时,自动初始化swiper observeParents:true//修改swiper的父元素时,自动初始化swiper });
The complete js code is as follows:
var myapp=angular.module("myApp",[]); //轮播图的控制器 myapp.controller("swiperController",function($scope,$http){ //请求轮播图路径 $http({ method: 'post', url: 'json/myJson.json' }).then(function successCallback(response) { $scope.imgSrcs = response.data.sites; var swiper = new Swiper('.swiper-container', {//重置轮播加载方法 pagination: '.swiper-pagination', slidesPerView: 1, paginationClickable: true, spaceBetween: 30, keyboardControl: true, nextButton: '.swiper-button-next', prevButton: '.swiper-button-prev', observer:true,//修改swiper自己或子元素时,自动初始化swiper observeParents:true//修改swiper的父元素时,自动初始化swiper }); }, function errorCallback(response) { // 请求失败执行代码 }); });
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
How to use jQuery to implement the toggle method of sliding left and right?
How to implement value-passing and URL encoding conversion in JS forms?
How to check the first one by default in the v-for loop checkbox through vue?
The above is the detailed content of How to solve the problem of not being able to slide using the Swiper plug-in in Angular.js. For more information, please follow other related articles on the PHP Chinese website!