您需要确保在您调用jQuery函数unslick
和slick
时,DOM已经被渲染。
你可以通过在$timeout
函数中使用这些函数来完成该操作(当然延迟为0)。这将确保$timeout
函数中的代码将在$diggest
循环完成后执行。
试试这个:
controller('myCtrl', ['$timeout',function($timeout){
var self = this;
self.data = [{
id: 1,
title: 'A title, representing part 1',
text: 'This is my TEXT 1'
},
{
id: 2,
title: 'Anoter interesting title that will grab your attention',
text:'...and even longer text!'
}];
self.next =function() {
$('.your-class').slickNext();
};
self.prev =function() {
$('.your-class').slickPrev();
};
self.add = function() {
var newID = self.data.length + 1;
self.data.push({
id:newID,
title:'A totally new object!',
text:'Dynamically added object to an existing array.'
});
$timeout(function(){
$('.your-class').unslick();
$('.your-class').slick({
autoplay: false,
autoplaySpeed: 1500,
swipeToSlide: true
});
});
};
}]);
UDATE
为了解决这个问题的任择议定书在波纹管的注释中描述的问题,它是一个与原来的问题我完全无关已经提出了这种解决方法:
$timeout(function(){
$('.your-class').unslick();
//You would think that `unslick` would take care of this, but it didn't so:
$('.your-class .slick-cloned, .your-class .slick-list.draggable').remove();
//this is a workaround, which proves that the issue was with the plugin
//nothing to do with the original question. In order to address this properly the
//OP should open a new question or a bug in the `unslick` plugin.
$('.your-class').slick({
autoplay: false,
autoplaySpeed: 1500,
swipeToSlide: true
});
});
使用指令来绑定浮动和不浮动将是个好主意。 – Senthil 2014-10-31 03:02:23
Senthil我非常感兴趣的是用指令解决这个问题,但是,我缺乏正确的知识。你有没有例子?谢谢。我目前的代码:http://jsbin.com/yocun/1/edit?html,js,console,output – uglycode 2014-10-31 10:18:52
我可以帮助你绝对,在现在的东西中间。我会尽快添加答案。 – Senthil 2014-10-31 10:59:43