简体   繁体   English

为什么在ng-show使用ng-hide可以适用于这两种情况

[英]why use ng-hide when ng-show can work for both situations

I have gone through so many questions like this but got this only solutions that ng-show by default hides the element and show it if condition is true and on the other hand ng-hide by default show the element and hide it when condition is true. 我已经通过像这么多的问题,走了这个 ,但得到了这个唯一的解决办法ng-show默认隐藏的元素并显示,如果条件为真,另一方面ng-hide默认显示的元素,隐藏它,当条件为真。

But my concern is the condition can be taken care of with ng-show or ng-hide only then why we use different things. 但我担心的是,使用ng-showng-hide可以解决这个问题,然后我们才会使用不同的东西。

For example 例如

I saw this somewhere in this code user is using ng-show and ng-hide both 我在这段代码中看到了这个用户正在使用ng-showng-hide两者

<div ng-init="isShow = 'one'">
    <a href="#" ng-click="isShow == 'one' ? isShow = 'two' : isShow = 'one'">
    <div ng-show="isShow=='one'">
        If One show this
    </div>
    <div ng-hide="isShow=='one'">
        If Two show this
    </div>
</div>

But according to me this can be achieved also with this code 但据我所知,这个代码也可以实现

<div ng-init="isShow = 'one'">
    <a href="#" ng-click="isShow == 'one' ? isShow = 'two' : isShow = 'one'">
    <div ng-show="isShow=='one'">
        If One show this
    </div>
    <div ng-show="isShow=='two'">
        If Two show this
    </div>
</div>

So what exactly is the difference between both the codes. 那么两个代码之间到底有什么区别呢。 There must be some specific difference if ng-show and ng-hide both exists. 如果存在ng-showng-hide则必须存在一些特定的差异。 Anyone know it? 谁知道呢?

Thanks in advance! 提前致谢!

I don't see any reason for this question to be downvoted - it's a valid thought. 我认为这个问题没有任何理由被贬低 - 这是一个有效的想法。 The reason is simple, though. 但原因很简单。 AngularJS has "declarative" as one of its core philosophies. AngularJS将“声明性”作为其核心理念之一。 If 90% of the time you want an element shown, but occasionally it should be hidden, ng-hide="thatcondition" clearly indicates when it should be hdiden. 如果90%的时间你想要一个元素显示,但偶尔它应该被隐藏, ng-hide="thatcondition"清楚地表明它何时应该是hdiden。 If most of the time it should be HIDDEN, then ng-show="thatrarecondition" is more readable. 如果大多数时候它应该是HIDDEN,那么ng-show="thatrarecondition"更具可读性。

Clear, readable code is an important principle in any framework, and especially in AngularJS. 清晰,可读的代码是任何框架中的重要原则,尤其是在AngularJS中。 The ! ! operator is narrow and easily missed, far more than any of the other comparisons like >, <, >=, <= , etc. Providing positive- and negative-visibility operators makes it much more readable what's going on here. 运算符很窄且很容易被遗漏,远远超过任何其他比较,例如>, <, >=, <=等。提供正面和负面可见性运算符使得它更具可读性。

An important detail to note is that both directives look for "truthy" values, not exactly-equal ones. 需要注意的一个重要细节是,两个指令都会查找“truthy”值,而不是完全相同的值。 JS is pretty vague about this, and sometimes that's an advantage. JS对此非常模糊,有时这是一个优势。 For example, suppose you have an object that may have a sub-object (a detail element). 例如,假设您有一个可能具有子对象(细节元素)的对象。 You might have the detail-display DIV written as follows: 您可能将详细显示DIV编写如下:

<div ng-show="{{ object.details }}">
    <!-- Render object.details here -->
</div>

This "truthy" comparison is also handy for the negative case. 这种“真实”的比较对于否定案例也很方便。 Suppose you want to HIDE an order-cancellation block in a sales system if the order has been shipped. 假设您希望在订单发货时隐藏销售系统中的订单取消块。 Consider: 考虑:

<div ng-hide="{{ order.shipped }}">
   Want to cancel this order? <a href="...">click here</a>
</div>

Why is this important? 为什么这很重要? Well, it means ANY non-undefined/null value for order.shipped will hide this block! 好吧,这意味着order.shipped的任何非undefined / null值都会隐藏这个块! That means if today, you set it as a true/false, it will work. 这意味着如果今天你将其设置为真/假,它将起作用。 But tomorrow you change it to a DATE that the order was shipped? 但是明天你将它更改为订单发货的日期? The rule will still work! 规则仍然有效! This makes it easy to code (and maintain) displays like this. 这样可以轻松编码(和维护)这样的显示。

This is actually pretty good explained in the docs. 这在文档中解释得非常好。 AngularJS ngShow AngularJS ngShow

Here's a quote from the docs: "The ngShow directive shows or hides the given HTML element based on the expression provided to the ngShow attribute. The element is shown or hidden by removing or adding the .ng-hide CSS class onto the element. The .ng-hide CSS class is predefined in AngularJS and sets the display style to none (using an !important flag)." 以下是文档的引用:“ ngShow指令根据提供给ngShow属性的表达式显示或隐藏给定的HTML元素。通过删除或添加.ng-hide CSS类来显示或隐藏元素。 .ng-hide CSS类在AngularJS中预定义,并将显示样式设置为none(使用!important标志)。“

Regarding ngHide: AngularJS ngHide 关于ngHide: AngularJS ngHide

Quote: "The ngHide directive shows or hides the given HTML element based on the expression provided to the ngHide attribute. The element is shown or hidden by removing or adding the ng-hide CSS class onto the element. The .ng-hide CSS class is predefined in AngularJS and sets the display style to none (using an !important flag)." Quote:“ ngHide指令根据提供给ngHide属性的表达式显示或隐藏给定的HTML元素。通过删除或添加ng-hide CSS类来显示或隐藏元素。.ng-hide CSS类在AngularJS中预定义并将显示样式设置为none(使用!important标志)。“

Also check out this for short but accurate explanation about different Angular DOM handling: http://www.w3schools.com/angular/angular_htmldom.asp 另外,请查看有关不同Angular DOM处理的简短但准确的解释: http//www.w3schools.com/angular/angular_htmldom.asp

So in the end they actually do the same thing. 所以最后他们实际上做了同样的事情。 And as far as I know, you should not use them in combination. 据我所知,你不应该结合使用它们。 If you would like to create multiple boolean values as parameters to either one of them you could do it like this: <div ng-show="value1 && !value2">Something</div> . 如果你想创建多个布尔值作为其中任何一个的参数,你可以这样做: <div ng-show="value1 && !value2">Something</div> Still I suggest that if you need more paramter values you should go with a function. 我仍然建议,如果你需要更多的参数值,你应该使用一个函数。

<div ng-show="ShowMe()">Content</div>

$scope.ShowMe = function(){
    return $scope.value && !$scope.value2;
}

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

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