简体   繁体   English

角ng-hide或ng-show? 为什么?

[英]Angular ng-hide or ng-show ? why?

I have seen this questions many times, but not once have I seen a normal answer. 我已经多次看到这个问题,但是没有一次看到正常的答案。

What is the difference between ng-hide and ng-show? ng-hide和ng-show有什么区别? is there one? 有一个吗? if so when to use one or the other ( I understand ng-if, I am asking only about this two). 如果是这样,什么时候使用其中一个(我理解ng-if,我只问这两个问题)。

I hope some one knows thanks. 我希望有人知道谢谢。

The ngShow directive shows or hides the given HTML element based on the expression provided to the ngShow attribute. ngShow指令根据提供给ngShow属性的表达式显示或隐藏给定的HTML元素。

The element is shown or hidden by removing or adding the .ng-hide CSS class onto the element. 通过删除.ng-hide CSS类到该元素上,可以显示或隐藏该元素。 The .ng-hide CSS class is predefined in AngularJS and sets the display style to none (using an !important flag). .ng-hide CSS类是在AngularJS中预定义的,并将显示样式设置为none(使用!important标志)。

ngHide is just syntaxic sugar when you need to invert the result of the expression. 当需要反转表达式的结果时, ngHide只是语法糖。

Basically, it would be the same to use ng-show="!testSomething()" and ng-hide="testSomething()" . 基本上,使用ng-show="!testSomething()"ng-hide="testSomething()"

All these directive s like ng-hide , ng-show and ng-if take a condition in the form of a boolean and show/hide from view according to true and false . 所有这些directive ng-hideng-showng-if采用boolean形式的条件,并根据truefalse从视图中显示/隐藏。 The logic they use to hide and show the view are different. 他们用来隐藏和显示视图的逻辑是不同的。

ng-show and ng-hide use internally CSS like this: ng-showng-hide内部使用CSS,如下所示:

display: none; // according to show or hide

But when we use ng-if- if condition is true : adds the element into DOM. 但是当我们使用ng-if- if条件为true :将元素添加到DOM中。 if condition is false : removes element from the DOM. 如果condition为false :从DOM中删除元素。

It means ng-show keeps the elements in the DOM and the cost of watcher remain the same, while the user isn't allowed to see the element in the DOM. 这意味着ng-show可以使DOM中的元素保持不变,并且观察者的成本保持不变,而用户则不允许查看DOM中的元素。 And if you replace ng-show with an ng-if, you might witness considerable improvements in the DOM because those extra watches are not there to take responsibility. 而且,如果用ng-show if替换ng-show ,则可能会见证DOM的显着改进,因为这些多余的手表不负责任。

If a particular element has ng-if/ng-show/ng-hide in the DOM, then it can have a performance impact. 如果特定元素在DOM中具有ng-if/ng-show/ng-hide ,则可能会对性能产生影响。 When you use ng-if , the app will be faster compared to ng-show/ng-hide. 当您使用ng-if ,与ng-show / ng-hide相比,该应用程序会更快。 But the difference is not noticeable. 但是区别并不明显。 But if you are using these directives in animations, then there is a possibility of performance issues. 但是,如果您在动画中使用这些指令,则可能会出现性能问题。

In short, if you can remove an element from DOM, then you should use ng-if, otherwise you should use ng-show or ng-hide. 简而言之,如果您可以从DOM中删除元素,则应使用ng-if,否则应使用ng-show或ng-hide。

ng-show 

Lets you make the display:block of the element when expression is true while ng-hide lets you set the display:none when the expression is true. 当表达式为true时,使您可以display:none元素的display:block;而当表达式为true时, ng-hide可以使您设置display:none They are just opposite to each other. 他们彼此相对。 Don't get confused. 不要感到困惑。 Use only one, ng-show as ng-show="exp" and ng-show="!exp" to show or hide an element 仅使用ng-showng-show="exp"ng-show="!exp"来显示或隐藏元素

As we know angularjs is a two way databinding. 众所周知,angularjs是两种方式的数据绑定。 ng-hide and ng-show is we used for two different purpose one for hide and another for show. 我们将ng-hide和ng-show用于两个不同的目的,一个用于隐藏,另一个用于显示。 for example if you use one method that returns true or false. 例如,如果您使用一种返回true或false的方法。 In any case hide of one element you want to show other then you need to use only one method for both hide and show. 无论如何,要显示一个元素的隐藏都需要隐藏和显示,而只需使用一种方法。

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

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