简体   繁体   English

ng-show和ng-hide有什么区别?

[英]What's the difference between `ng-show` and `ng-hide`?

These attributes are both given either a true or false value, so what difference is there between them? 这些属性都被赋予了truefalse值,那么它们之间有什么区别? It would make sense if there weren't values for them. 如果没有价值,那将是有道理的。

Am I missing something? 我想念什么吗?

With ng-show the element is shown if the expression is true , it will hide if it is false 使用ng-show ,如果表达式为true ,则ng-show元素,如果为false ,则将隐藏

On the other hand with ng-hide the element is hidden if the expression is true and it will be shown if it is false . 在另一方面与ng-hide如果表达式为元件被隐藏,将示出如果是假的

Just two sides of the same coin. 同一枚硬币的两侧。

On a project I was working on before, I found having the option of both ng-show and ng-hide useful. 在我之前从事的项目中,我发现同时具有ng-showng-hide非常有用。 The reason being is because I had a link in my navbar that was only supposed to show if the user was on a specific view. 原因是因为我的导航栏中有一个链接,该链接仅应显示用户是否在特定视图上。 Here is that scenario: 这是这种情况:

<li ng-hide="isActive('/about') || isActive('/contact')" ng-class="{ 'vert-nav-active': isActive('/investigator')}" class="top-buffer">
<a href="#/investigator" class="buff-sides navListLinks">Investigator Portal</a>
</li>

Now, you might say, well you could just make the isActive('/about') || isActive('/contact') 现在,您可能会说,很好,您可以使isActive('/about') || isActive('/contact') isActive('/about') || isActive('/contact') return the opposite Boolean and change the ng-hide to ng-show and every thing will stay the same but as you can see I'm also using this function to determine which link I'm on. isActive('/about') || isActive('/contact')返回相反的布尔值并将ng-hide更改为ng-show ,所有内容都将保持不变,但是正如您所看到的,我也正在使用此函数来确定我在哪个链接上。 If I reverse this logic, it will look like I'm on every link except the actual link I'm on. 如果我颠倒了这个逻辑,那么看起来好像我在每个链接上,但我所连接的实际链接除外。 Granted I could write another function for the ng-show but I like reusing code that's already there. 当然,我可以ng-show编写另一个函数,但是我喜欢重用已经存在的代码。

值得一提的是ng-if ,它像ng-showng-hide一样接受布尔表达式,但不仅仅是根据表达式显示/隐藏元素,如果表达式为false并从DOM中完全删除了元素,如果表达式为true则元素返回DOM

i have a good scenario, let say you want to show one attribute or another depending on a validation, but not both, so using both ng-show and ng-hide like this: 我有一个很好的方案,假设您要根据验证显示一个或另一个属性,但不能同时显示两个,因此请同时使用ng-show和ng-hide:

<div ng-show="validation"></div>
<div ng-hide="validation"></div>

if the validation is true it would show the first div, but if it is false it would show the second div... this can be done in many more ways but is a cleaver solution for a tricky problem :D 如果验证是正确的,它将显示第一个div,但是如果为假,则将显示第二个div ...这可以通过许多其他方式来完成,但这是一个棘手的解决方案:D

ng-show will only display the element IF the expression is true... 如果表达式为真,ng-show将仅显示元素。

<span ng-show="true">Will Show </span>
<span ng-show="false">Will not Show </span>

ng-hide will not display the element IF the expression is true, but will display if the expression is false. 如果表达式为true,则ng-hide将不会显示该元素,但如果表达式为false,则将显示。

<span ng-hide="true">Will not display</span>
<span ng-hide="false">Will display</span>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM