解決Angular.js中使用Swiper插件不能滑動的問題
我們都知道swiper是交互體驗十分好的輪播插件
但是通過angular(ng-repeat)循環(huán)出來的swiper不能輪播的解決方案
通常我們都是通過以下方法來執(zhí)行:
html
<div class="swiper-container" ng-controller="swiperController"> <div class="swiper-wrapper"> <div class="swiper-slide" ng-repeat="informarion in imgSrcs"> <img ng-src="{{informarion.sliderSrc}}" /> </div> </div> <!-- Add Pagination --> <div class="swiper-pagination"></div> <!-- Add Arrows --> </div>
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) { // 請求失敗執(zhí)行代碼 }); });
可是還是不行,注意:如果是在json中獲取數(shù)據(jù),要把輪播js代碼寫在獲取數(shù)據(jù)中,因為他是先獲取數(shù)據(jù)才執(zhí)行輪播的,如果你把他放在外部,實行輪播數(shù)據(jù)獲取不到。
所以解決方案便是將swiper的初始化方法放到$http請求里面執(zhí)行,
將如下代碼加到function successCallback()方法里面即可實現(xiàn)輪播
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 });
完整的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; 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) { // 請求失敗執(zhí)行代碼 }); });
以上這篇解決Angular.js中使用Swiper插件不能滑動的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Angular實現(xiàn)較為復雜的表格過濾,刪除功能示例
這篇文章主要介紹了Angular實現(xiàn)較為復雜的表格過濾,刪除功能,結合實例形式分析了AngularJS針對表格的排序、查詢匹配、頁面元素屬性動態(tài)修改等相關操作技巧,需要的朋友可以參考下2017-12-12如何處理Angular?錯誤消息ERROR?Error?NullInjectorError?No?provid
這篇文章主要介紹了如何處理Angular?錯誤消息ERROR?Error?NullInjectorError?No?provider?for?XX2023-07-07用angular實現(xiàn)多選按鈕的全選與反選實例代碼
本篇文章主要介紹了用angular實現(xiàn)多選按鈕的全選與反選實例代碼,非常具有實用價值,需要的朋友可以參考下2017-05-05詳解為Angular.js內(nèi)置$http服務添加攔截器的方法
所謂攔截器就是在目標達到目的地之前對其進行處理以便處理結果更加符合我們的預期。Angular的$http攔截器是通過$httpProvider.interceptors數(shù)組定義的一組攔截器,每個攔截器都是實現(xiàn)了某些特定方法的Factory。本文就介紹了為Angular.js內(nèi)置$http服務添加攔截器的方法。2016-12-12