您需要确保在调用 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
});
});
};
}]);
更新日期
为了解决 OP 在下面的 cmets 中描述的问题,并且它与原始问题完全无关,我想出了这个解决方法:
$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
});
});