我写了一些用jquery切换下拉菜单的代码。所以我想在angularJs中编写代码。我不太了解angularjs。
Html模板
<div id="add" class="modal category modal-fixed-footer">
<div class="modal-content">
<h4>Categories</h4>
<div class="wrapper">
<ul class="collection main first-level">
<!-- FIRST LEVEL ITEM (WOMEN) -->
<li class="collection-item">
<span class="item-wrapper">
<span>Women<i class="material-icons">arrow_drop_down</i></span>
</span>
<p class="checkbox">
<input type="checkbox" class="filled-in" id="1"/>
<label for="1"></label>
</p>
<ul class="collection second-level">
<!-- SECOND LEVEL ITEM -->
<li class="collection-item ">
<span class="item-wrapper">
<span>Women's Clothing<i class="material-icons">arrow_drop_down</i></span>
</span>
<p class="checkbox">
<input type="checkbox" class="filled-in" id="2" />
<label for="2"></label>
</p>
<ul class="collection third-level">
<!-- SECOND LEVEL ITEM -->
<li class="collection-item"><span class="item-wrapper">Blazers </span><p class="checkbox"><input type="checkbox" class="filled-in" id="3" /><label for="3"></label></p></li>
<li class="collection-item"><span class="item-wrapper">Jeans </span><p class="checkbox"><input type="checkbox" class="filled-in" id="6" /><label for="6"></label></p></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="modal-footer">
<a href="#!" class="modal-action modal-close waves-effect btn-flat ">Save</a>
<a href="#!" class="modal-action modal-close waves-effect btn-flat ">Cancel</a>
</div>
</div>
Jquery代码
$( ".collection-item > span" ).click(function() {
$(this).next(".collection").slideToggle( "medium" );
$(this).next().next(".collection").slideToggle( "medium" );
var icon = $(this).find('i').text();
if (icon == "arrow_drop_up") {
$(this).find('i').text("arrow_drop_down");
} else {
$(this).find('i').text("arrow_drop_up");
}
});
我们如何在angularJs中编写相同的功能代码而不是Jquery。请做好。
答案 0 :(得分:1)
每当有与dom相关的操作时,最好的选择是创建一个指令(也是可重用的)。就像这里toggle-me
<div id="add" class="modal category modal-fixed-footer">
<div class="modal-content">
<h4>Categories</h4>
<div class="wrapper">
<ul class="collection main first-level">
<!-- FIRST LEVEL ITEM (WOMEN) -->
<li class="collection-item">
<span class="item-wrapper" toggle-me>
<span>Women<i class="material-icons">arrow_drop_down</i></span>
</span>
<p class="checkbox">
<input type="checkbox" class="filled-in" id="1" />
<label for="1"></label>
</p>
<ul class="collection second-level">
<!-- SECOND LEVEL ITEM -->
<li class="collection-item ">
<span class="item-wrapper" toggle-me>
<span>Women's Clothing<i class="material-icons">arrow_drop_down</i></span>
</span>
<p class="checkbox">
<input type="checkbox" class="filled-in" id="2" />
<label for="2"></label>
</p>
<ul class="collection third-level">
<!-- SECOND LEVEL ITEM -->
<li class="collection-item"><span class="item-wrapper">Blazers </span>
<p class="checkbox">
<input type="checkbox" class="filled-in" id="3" />
<label for="3"></label>
</p>
</li>
<li class="collection-item"><span class="item-wrapper">Jeans </span>
<p class="checkbox">
<input type="checkbox" class="filled-in" id="6" />
<label for="6"></label>
</p>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="modal-footer">
<a href="#!" class="modal-action modal-close waves-effect btn-flat ">Save</a>
<a href="#!" class="modal-action modal-close waves-effect btn-flat ">Cancel</a>
</div>
</div>
角度代码
angular.module('App', [])
.directive('toggleMe', function() {
return {
restrict: 'E',
link: function(scope, ele, attr) {
ele.on('click', function() {
ele.next('.collection').slideToggle('medium');
ele.next().next('.collection').slideToggle('medium');
var icon = ele.find('i').text();
if (icon === 'arrow_drop_up') {
ele.find('i').text('arrow_drop_down');
} else {
ele.find('i').text('arrow_drop_up');
}
});
}
};
});
在指令中,您可以根据自己的要求添加逻辑。