Home > Web Front-end > JS Tutorial > body text

How to solve the problem of not being able to slide using the Swiper plug-in in Angular.js

亚连
Release: 2018-06-04 14:03:30
Original
1544 people have browsed it

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>
Copy after login

js

var myapp=angular.module("myApp",[]);
 //轮播图的控制器
 myapp.controller("swiperController",function($scope,$http){
  //请求轮播图路径
  $http({
   method: &#39;post&#39;,
   url: &#39;json/myJson.json&#39;
  }).then(function successCallback(response) {
   $scope.imgSrcs = response.data.sites;
  }, function errorCallback(response) {
   // 请求失败执行代码
  });
 });
Copy after login

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(&#39;.swiper-container&#39;, {//重置轮播加载方法
    pagination: &#39;.swiper-pagination&#39;,
    slidesPerView: 1,
    paginationClickable: true,
    spaceBetween: 30,
    keyboardControl: true,
    nextButton: &#39;.swiper-button-next&#39;,
    prevButton: &#39;.swiper-button-prev&#39;,
    observer:true,//修改swiper自己或子元素时,自动初始化swiper
    observeParents:true//修改swiper的父元素时,自动初始化swiper
   });
Copy after login

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(&#39;.swiper-container&#39;, {//重置轮播加载方法
    pagination: &#39;.swiper-pagination&#39;,
    slidesPerView: 1,
    paginationClickable: true,
    spaceBetween: 30,
    keyboardControl: true,
    nextButton: &#39;.swiper-button-next&#39;,
    prevButton: &#39;.swiper-button-prev&#39;,
    observer:true,//修改swiper自己或子元素时,自动初始化swiper
    observeParents:true//修改swiper的父元素时,自动初始化swiper
   });
  }, function errorCallback(response) {
   // 请求失败执行代码
  });
 });
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template