尝试让 3d 派对 jQuery 库使用两个非常简单的自定义指令:
无法让 ng-click 工作,并且不确定如何从链接函数中的重复元素中获取数据。
当您单击幻灯片时,其名称和隐藏数据应附加在其下方的列表中。
angular.module('sm', [])
.directive('selector', function () {
return {
restrict: "E",
template: '<div class="swiper-wrapper">' +
'<div class="swiper-slide" ng-repeat="slide in slides">' +
'<h1 ng-click="selected(slide)">{{ slide.name }}</h1>' +
'</div></div>',
replace: true,
controller: ['$scope', '$timeout', function ($scope, $timeout) {
$scope.slides = [{
name: 'one',
hidden: 'kittens'
}, {
name: 'two',
hidden: 'puppies'
}, {
name: 'three',
hidden: 'bacon'
}];
$timeout(function () { // important!
$.swiper.init();
});
// ng-click never fired due to the jQuery slider plugin
$scope.selected = function (data) {
console.log('ng-click called $scope.selected');
$scope.$broadcast('slideSelected', data);
};
}],
link: function linkFn(scope, lElement, attrs) {
lElement.on('click', function (el) {
console.log('lElement on click called');
// how do I get access to the clicked element's data?
scope.$broadcast('slideSelected', el);
$
})
}
}
})
.directive('selected', function () {
return {
restrict: "E",
template: '<ul>' +
'<li ng-repeat="selection in selected">{{ selection }}</li>' +
'</ul>',
replace: true,
controller: ['$scope', function ($scope) {
var selected = ['Add to me', 'please'];
$scope.selected = selected;
$scope.$on('slideSelected', function (data) {
$scope.$apply(function () {
selected.push(selected);
})
});
}],
}
})
.controller('MyCtrl', function ($scope) {});
$.swiper = {
init: function () {
var mySwiper = $('.swiper-container').swiper({
mode: 'horizontal',
loop: true
});
}
};