[英]how to declare ng-show and ng-hide in one div
In my project I had a requirement to use both ng-show
and ng-hide
in one div
. 在我的项目中,我需要在一个div
同时使用ng-show
和ng-hide
。 I felt that's a bad practice. 我觉得这是一个不好的做法。
html code: HTML代码:
<div ng-hide="itemDIv2" ng-show="itemDIv2">
<input type="text" placeholder="Item Name" ng-model="itemname">
<div>
<input type="submit" class="btn" value="Save" ng-click="subcatiems()>
</div>
</div>
another div: 另一个div:
<div><button class='btn' ng-click="catitems2()">Add items to Category</button></div>
controller: 控制器:
$scope.catitems2 = function(){
return $scope.itemDIv2 = true;
}
how to take a condition that initially it is on hide and when the button is clicked i want to make ng-show="itemDIv2"
to true
so that I can show the div
one more tome. 如何采取最初它是隐藏的条件和单击按钮时我想使ng-show="itemDIv2"
为true
以便我可以再显示一个div
。
You don't need both ng-show
and ng-hide
on same div to acheive this functionality. 您不需要同一个div上的ng-show
和ng-hide
来实现此功能。 You can toggle the scope variable $scope.itemDIv2
on button click. 您可以在按钮单击时切换范围变量$scope.itemDIv2
。
<div class="settings-heading " style="background-color: #f2f2f2;"
ng-show="itemDIv2" ng-init='itemDIv2=true'>
Demo text
</div>
<div>
<button class='btn' ng-click="itemDIv2 = !itemDIv2" >
Add items to Category
</button>
</div>
Working JSBin: https://jsbin.com/vaduwan/2/edit?html,js,console,output 使用JSBin: https ://jsbin.com/vaduwan/2/edit ? html,js,console,output
To expand on on @vp_arth's comment, you don't need both. 要扩展@ vp_arth的评论,您不需要两者。 But you're on the right track with the boolean flag. 但是你在布尔标志的正确轨道上。
I would suggest making these changes: 我建议做出这些改变:
Add this object to the controller scope: 将此对象添加到控制器范围:
$scope.ui = { showDiv: false };
And in the template, change the button to: 在模板中,将按钮更改为:
button ng-click="ui.showDiv = !ui.showDiv" /
And instead of both ng-show and ng-hide, use: 而不是ng-show和ng-hide,使用:
ng-show="ui.showDiv"
This way you don't need a catitems2() function, and the div or what you want to show starts off hidden. 这样你就不需要一个catitems2()函数了,div或你想要显示的东西开始隐藏。
Here's a working JSFiddle of the changes: 这是一个改进的JSFiddle:
http://jsfiddle.net/Lvc0u55v/6534/ http://jsfiddle.net/Lvc0u55v/6534/
Both ngShow
and ngHide
just add/remove NG_HIDE_CLASS
class to element. ngShow
和ngHide
只是为元素添加/删除NG_HIDE_CLASS
类。
Try read the sources to understand that: ngShowHide 尝试阅读来源以了解: ngShowHide
Use one boolean scope variable and set it to neccesary value with one of that directives. 使用一个布尔范围变量,并使用其中一个指令将其设置为neccesary值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.