javascript - 需要帮助编写 Else/If 语句部分 - 如果单击的 li 没有子项,则重置 li 图标的状态
问题描述
下面的代码在没有 (2nd)else if 语句的情况下工作正常,一旦我单击 li,图标将变为 arrow_dropdown,如果我再次单击,它将变为材料设计的展开_more 图标。需要帮助写作-> 当我单击没有子项的 li 时,将所有 li 图标(从 arrow_dropdown)重置为展开_more。基本上现在发生的事情是当我单击没有子菜单的 li 时,带有 submenuItems 的 li 关闭,但是当 li 关闭时,图标仍然保持为 arrow_dropdown。
$scope.toggleSubNav = function(item) {
if (item.subItems) {
$scope.itemClicked = $scope.clicked = !$scope.clicked;
//if clicked first time change unfold to arrow_dropdown i.e. open state
if ($scope.itemClicked === true && item.subicon === 'unfold_more') {
item.subicon = 'arrow_drop_down'
}
//need help writing this part
//if clicked on li with no subItems, change icon of li's to unfold_more i.e. closed
else if ($scope.itemClicked === true && item.subItems === false) {
item.subicon = 'unfold_more'
}
// if clicked 2nd time change arrow_dropdown to unfold i.e. closed state
else {
item.subicon = 'unfold_more'
}
}
};
我的控制器看起来像这样 -
app.controller('NavigationController', function($scope) {
// Must use a wrapper object, otherwise "activeItem" won't work
$scope.states = {};
$scope.states.activeItem = '';
$scope.items = [{
id: 'item1',
title: 'Dashboard',
icon: 'dashboard',
subicon: 'unfold_more',
route: 'dashboard',
subItems: [{
title: 'Page Visitors',
subroute: 'page-visitors',
},
{
title: 'Post Performace',
subroute: 'post-performance',
},
{
title: 'Team Overall',
// subroute: '/team-overall',
}
]
}, {
id: 'item2',
title: 'Calendar',
icon: 'calendar_today',
}, {
id: 'item3',
title: 'Inbox',
icon: 'inbox',
subicon: 'unfold_more',
subItems: [{
title: 'New Mail',
// subroute: '/new-mail',
},
{
title: 'Scoial',
// subroute: '/new-social',
},
{
title: 'Updates',
// subroute: '/new-updates',
}
]
}, {
id: 'item4',
title: 'Invoicing',
icon: 'insert_drive_file',
}];
});
在这里你可以看到我的 html
<ul class="navigation">
<div class="" layout="column">
<li md-ink-ripple="#ebeef4"
ng-repeat="item in items"
class="item {{item.id}}"
ng-click="states.activeItem=item.title;
toggleSubNav(item)">
<span>
<i class="material-icons md-24 center-icons">{{item.icon}}</i>
</span>
<a class="title" ng-href="#!{{item.route}}">{{item.title}}</a>
<span>
<!--change the icons {{item.subicon}} inside i tag -->
<i ng-class="icon"
class="material-icons md-18 subicon-openstate"
id="test-icons">{{item.subicon}}</i>
</span>
<ul class="submenu" id="submenu-item-hover" ng-if="itemClicked && states.activeItem === item.title && item.subItems">
<li ng-repeat="subItem in item.subItems" class="subItem">
<!-- ng-href="#!{{subItem.subroute}}" -->
<a href="#!{{subItem.subroute}}">{{subItem.title}}</a>
</li>
</ul>
</li>
</div>
</ul>
解决方案
推荐阅读
- angular - 在 Angular 中多次使用不同名称和不同验证的父组件中的子组件
- c - 如何修复'对'main'collect2的未定义引用:错误:ld返回1退出状态'
- php - 如何限制 Firebase 电话号码验证短信仅发送到已注册的电话号码?
- javascript - Firebase - 为什么未将声明添加到用户属性中?
- javascript - 如果在 JS 中满足输入条件,则禁用或启用按钮,而输入是自动生成的
- java - 无法将APK上传到google play
- php - Laravel:CORS 缺少允许来源
- timescaledb - TimescaleDB 连续聚合视图不会在旧存储桶上刷新
- android - 仅当通过公司 intune 门户打开时,应用程序会因 intune sdk 引用而崩溃
- logic - 给出逻辑应用状态的用户响应?