我需要将这些代码添加到我的AngularJS应用程序中。我该怎么做?我知道有Angular手风琴实现,但这很容易,而且效果很好。
$(function() {
var Accordion = function(el, multiple) {
this.el = el || {};
this.multiple = multiple || false;
// Variables privadas
var links = this.el.find('.link');
// Evento
links.on('click', {el: this.el, multiple: this.multiple}, this.dropdown)
}
Accordion.prototype.dropdown = function(e) {
var $el = e.data.el;
$this = $(this),
$next = $this.next();
$next.slideToggle();
$this.parent().toggleClass('open');
if (!e.data.multiple) {
$el.find('.submenu').not($next).slideUp().parent().removeClass('open');
};
}
var accordion = new Accordion($('#accordion'), false);
});
答案 0 :(得分:0)
经过多次失误后,我自己想出了一个解决方案。 如果有人会遇到类似问题我的代码是:
angular.module('frontendApp')
.directive('accordion', function () {
return {
link: function (scope, element) {
var links = element.find('.link');
links.on('click', function(){
var $this = $(this);
var $next = $this.next();
$next.slideToggle();
$this.parent().toggleClass('open');
});
}
}
});
这样做了。