我正在尝试构建一个 AngularJS 指令来切换我的移动导航,但它不起作用。我究竟做错了什么?
该指令
app.directive('mobileDirective', function(){
return {
restrict: 'A',
link: function (scope, element, attrs){
element.bind('onclick', function(){
var status = element[0].className = "is-o";
if(status){
element[0].className = ".mobile-nav-toggle mobile-nav";
}else{
element[0].className = ".mobile-nav-toggle mobile-nav is-o";
}
});
}
}
});
index.html
<header data-ng-controller="navController">
<div class="header-position">
<a href="/" class="logo img-responsive"><img src="./assets/images/codehan_logo_raw.svg" class="mobile-logo" /></a>
<nav id="nav" class="site-nav">
<ul>
<li data-ng-repeat="item in menu_items">
<a data-ng-class="{ active: isActive('/{{item.name}}')}" href="{{item.name}}">{{item.name}}</a>
</li>
</ul>
</nav>
<div class="mobile-nav-toggle mobile-directive"><span></span></div>
</div>
<div id="m-nav" class="mobile-nav mobile-directive">
<ul>
<li data-ng-repeat="item in menu_items">
<a data-ng-class="{ active: isActive('/{{item.name}}')}" href="{{item.name}}">{{item.name}}</a>
</li>
</ul>
</div>
</header>
使用 jQuery,它看起来像这样并且工作正常
function mobileNav(){
$('.mobile-nav-toggle').on('click', function(){
var status = $(this).hasClass('is-o');
if(status){
$('.mobile-nav-toggle, .mobile-nav').removeClass('is-o');
}else{
$('.mobile-nav-toggle, .mobile-nav').addClass('is-o');
}
});
}