我正在使用 AngularJS、jQuery、HTML、CSS 和 Bootstrap 制作一个 Web 应用程序,我想从位于 Apache2 服务器中的 JSON 中挑选一些图像链接,并使用它们在我的主页中呈现这些图像. 我也想像在旋转木马中一样滑动它们。为了完成这项工作,我正在尝试使用iDangero.us Swiper。
当我用 3 个分开的 div 选择我的图像时,我没有问题。我得到我的图像,然后我通常可以随意滑动它们。我这样做如下所示:
主要.html:
<div ng-init="initGetRequestMain()">
<div class="swiper-slide" ng-click="swipers()"
style="{{data.background1}}"></div>
<div class="swiper-slide" ng-click="swipers()"
style="{{data.background2}}"></div>
<div class="swiper-slide" ng-click="swipers()"
style="{{data.background3}}"></div>
<script src="scripts/custom/main/swipers.js"></script>
</div>
我使用 Swiper 从一个图像滑动到另一个图像,它似乎可以正常工作。这是一个 jQuery 插件,你可以在这个链接上看到一些演示。
Swipers.js:
angular.module('swipers', [])
.controller('',[
$(document).ready(function (){
var swiper = new Swiper('.swiper-container',{
direction: 'horizontal',
pagination: '.swiper-pagination',
paginationClickable: true
})
})]);
杰森:
"background1":"background-image: url(images/img1.jpg)",
"background2":"background-image: url(images/img2.jpg)",
"background3":"background-image: url(images/img3.jpg)"
主控制器.js:
myApp.controller('MainController', ["$scope","$http",
function($scope,$http){
$scope.initGetRequestMain = function(){
$http.get('http://localhost/main.json').success(function(data){
$scope.data=data;
})
}
}]);
问题是,当我尝试使用ng-repeat
而不是 3 个单独的 div 时,我再也看不到它们,并且我的 Swiper 脚本在它们完全加载之前触发。我的控制台或 JSON 中没有错误(使用 JSONLint 验证)。下面,我在这两种情况下添加了我的输出的 2 个屏幕截图。
使用 3 个单独的 div:
不使用 ng-repeat:
这是我尝试使ng-repeat
工作保持与以前相同的控制器和相同的 Swiper 脚本的代码:
主要.html:
<div ng-init="initGetRequestMain()">
<div ng-repeat="slide in data.slides" isLoaded="">
<div class="swiper-slide" style="{{slide.background}}"
ng-click="swipers()"></div>
</div>
<script type="text/javascript-lazy" src="scripts/custom/main/swipers.js"></script>
</div>
mainJson.json:
"slides":[
{"background":"background-image:url('images/img1.jpg')"},
{"background":"background-image:url('images/img2.jpg')"},
{"background":"background-image: url('images/img3.jpg')"}
],
为了在触发脚本之前加载我的图像,我正在尝试使用 2 个自定义指令。
isLoaded
告诉我最后一个ng-repeat
元素何时加载并设置pageIsLoaded = true;
:
myApp.directive('isLoaded', function (){
return{
scope:true,
restrict: 'A', //Attribute type
link: function (scope, elements, arguments){
if (scope.$last === true) {
scope.pageIsReady = true;
console.log('page Is Ready!');
}
}
}
})
gettingTheScript
等待pageIsLoaded = true;
并加载脚本:
myApp.directive('src', function (){
return{
scope:true,
restrict: 'A', //Attribute type
link: function (scope, elements, arguments){
scope.$on(pageIsReady===true, function(){
if (attr.type === 'text/javascript-lazy'){
scope.scriptLink = arguments.src;
}
})
},
replace: true, //replaces our element
template: '{{scriptLink}}'
}
})
他们似乎没有解决我的问题。console.log('page Is Ready!');
做第一个的时候我也看不到。
当我必须在页面加载后触发像 Swiper 这样的脚本以避免此类问题时,我只是遇到了一些麻烦。我的图像似乎没有高度。我认为这个问题是由于ng-repeat
在触发我的脚本之前没有完全加载造成的。
我究竟做错了什么?有更好的解决方案吗?