您需要確保在您調用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