[英]calling ng-show on specific element based on parameter (angular)
In my menu I have a lot of buttons that open submenus. 在我的菜单中,我有很多打开子菜单的按钮。
Since I don't want a massive switch in my code that checks which button is pressed and which menu to animate I wanted to try the following: 由于我不想在代码中进行大量切换,以检查按下哪个按钮以及要设置动画的菜单,我想尝试以下操作:
In the HTML I call the function toggleVis with as a parameter the name of the element to be toggled: 在HTML中,我将函数toggleVis作为参数调用要切换的元素的名称:
<ul class="sidebar-bottom-list" ng-controller="MenuController">
<li>
<a href="#/dossier" ng-click="toggleVis(showSubmenuDos); go('/dossier')" ng-class="class" class="sidemenuItem"><span class="glyphicon glyphicon-folder-open"> </span>Dossiers</a>
<div class="list-group narrow-list-group no-padding-bottom slider-top-bottom" ng-show="showSubmenuDos" ng-class="active">
<a href="#" class="list-group-item" ng-class="dosMenuItems(0)">lijst</a>
<a href="#" class="list-group-item" ng-class="dosMenuItems(1)">algemeen</a>
<a href="#" class="list-group-item" ng-class="dosMenuItems(2)">partijen</a>
<a href="#" class="list-group-item" ng-class="dosMenuItems(3)">documenten</a>
<a href="#" class="list-group-item" ng-class="dosMenuItems(4)">notas</a>
<a href="#" class="list-group-item" ng-class="dosMenuItems(5)">royementen</a>
<a href="#" class="list-group-item" ng-class="dosMenuItems(6)">urenregistratie</a>
<a href="#" class="list-group-item" ng-class="dosMenuItems(7)">voortgang</a>
</div>
</li>
<!-- imagine more menu items like this one -->
Then in my main.js I have the toggleVis function like this: 然后在我的main.js中我有这样的toggleVis函数:
app.controller('MenuController', function($scope, $location) {
//can be triggered by ng-click="go('/path')"
$scope.go = function(path) {
$location.path(path);
};
//toggle visibillity
$scope.toggleVis = function(param) {
//$scope.{param} = !$scope.{param}
alert(param)
$scope.param = !$scope.param;
//param.ngShow = !param.ngShow;
//$scope.this = !$scope.this;
}
});
As you can see I have tried some things, but nothing works. 正如你所看到的,我尝试了一些东西,但没有任何作用。
Is there a way to do what I want, or is there a way to do this even better? 有办法做我想做的事,还是有办法做得更好?
I am looking for a well explained answer. 我正在寻找一个解释得很好的答案。
The easiest way to toggle views is the below example. 切换视图的最简单方法是以下示例。 Just switch value on click with
var = !var
and combine this with ng-show
: 只需使用
var = !var
切换值,然后将其与ng-show
结合使用:
var myApp = angular.module('myApp',[]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp"> <button ng-click="show = !show">Toggle</button> <p ng-show="show">I am getting shown and hidden!</p> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.