Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 246c143

Browse files
matskomhevery
authored andcommitted
fix(ngShowHide): change the .ng-hide CSS class to use an !important flag
1 parent 85f0d4a commit 246c143

File tree

2 files changed

+139
-21
lines changed

2 files changed

+139
-21
lines changed

css/angular.css

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
@charset "UTF-8";
22

33
[ng\:cloak], [ng-cloak], [data-ng-cloak], [x-ng-cloak],
4-
.ng-cloak, .x-ng-cloak {
4+
.ng-cloak, .x-ng-cloak,
5+
.ng-hide {
56
display: none !important;
67
}
78

89
ng\:form {
910
display: block;
1011
}
11-
12-
.ng-hide {
13-
display: none;
14-
}

src/ng/directive/ngShowHide.js

+137-16
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,77 @@
55
* @name ng.directive:ngShow
66
*
77
* @description
8-
* The `ngShow` and `ngHide` directives show or hide a portion of the DOM tree (HTML)
9-
* conditionally based on **"truthy"** values evaluated within an {expression}. In other
10-
* words, if the expression assigned to **ngShow evaluates to a true value** then **the element is set to visible**
11-
* (via `display:block` in css) and **if false** then **the element is set to hidden** (so display:none).
12-
* With ngHide this is the reverse whereas true values cause the element itself to become
13-
* hidden.
8+
* The `ngShow` directive shows and hides the given HTML element conditionally based on the expression
9+
* provided to the ngShow attribute. The show and hide mechanism is a achieved by removing and adding
10+
* the `ng-hide` CSS class onto the element. The `.ng-hide` CSS class is a predefined CSS class present
11+
* in AngularJS which sets the display style to none (using an !important flag).
1412
*
13+
* <pre>
14+
* <!-- when $scope.myValue is truthy (element is visible) -->
15+
* <div ng-show="myValue"></div>
16+
*
17+
* <!-- when $scope.myValue is falsy (element is hidden) -->
18+
* <div ng-show="myValue" class="ng-hide"></div>
19+
* </pre>
20+
*
21+
* When the ngShow expression evaluates to false then the ng-hide CSS class is added to the class attribute
22+
* on the element causing it to become hidden. When true, the ng-hide CSS class is removed
23+
* from the element causing the element not to appear hidden.
24+
*
25+
* ## Why is !important used?
26+
*
27+
* You may be wondering why !important is used for the .ng-hide CSS class. This is because the `.ng-hide` selector
28+
* can be easily overridden by heavier selectors. For example, something as simple
29+
* as changing the display style on a HTML list item would make hidden elements appear visible.
30+
* This also becomes a bigger issue when dealing with CSS frameworks.
31+
*
32+
* By using !important, the show and hide behavior will work as expected despite any clash between CSS selector
33+
* specificity (when !important isn't used with any conflicting styles). If a developer chooses to override the
34+
* styling to change how to hide an element then it is just a matter of using !important in their own CSS code.
35+
*
36+
* ### Overriding .ng-hide
37+
*
38+
* If you wish to change the hide behavior with ngShow/ngHide then this can be achieved by
39+
* restating the styles for the .ng-hide class in CSS:
40+
* <pre>
41+
* .ng-hide {
42+
* //!annotate CSS Specificity|Not to worry, this will override the AngularJS default...
43+
* display:block!important;
44+
*
45+
* //this is just another form of hiding an element
46+
* position:absolute;
47+
* top:-9999px;
48+
* left:-9999px;
49+
* }
50+
* </pre>
51+
*
52+
* Just remember to include the important flag so the CSS override will function.
53+
*
54+
* ## A note about animations with ngShow
55+
*
56+
* Animations in ngShow/ngHide work with the show and hide events that are triggered when the directive expression
57+
* is true and false. This system works similar to the animation system present with ngClass, however, the
58+
* only difference is that you must also include the !important flag to override the display property so
59+
* that you can perform an animation when the element is hidden during the time of the animation.
60+
*
61+
* <pre>
62+
* //
63+
* //a working example can be found at the bottom of this page
64+
* //
65+
* .my-element.ng-hide-add, .my-element.ng-hide-remove {
66+
* transition:0.5s linear all;
67+
* display:block!important;
68+
* }
69+
*
70+
* .my-element.ng-hide-add { ... }
71+
* .my-element.ng-hide-add.ng-hide-add-active { ... }
72+
* .my-element.ng-hide-remove { ... }
73+
* .my-element.ng-hide-remove.ng-hide-remove-active { ... }
74+
* </pre>
1575
*
1676
* @animations
17-
* show - happens after the ngShow expression evaluates to a truthy value and the contents are set to visible
18-
* hide - happens before the ngShow expression evaluates to a non truthy value and just before the contents are set to hidden
77+
* addClass: .ng-hide - happens after the ngShow expression evaluates to a truthy value and the just before contents are set to visible
78+
* removeClass: .ng-hide - happens after the ngShow expression evaluates to a non truthy value and just before the contents are set to hidden
1979
*
2080
* @element ANY
2181
* @param {expression} ngShow If the {@link guide/expression expression} is truthy
@@ -97,16 +157,77 @@ var ngShowDirective = ['$animate', function($animate) {
97157
* @name ng.directive:ngHide
98158
*
99159
* @description
100-
* The `ngShow` and `ngHide` directives show or hide a portion of the DOM tree (HTML)
101-
* conditionally based on **"truthy"** values evaluated within an {expression}. In other
102-
* words, if the expression assigned to **ngShow evaluates to a true value** then **the element is set to visible**
103-
* (via `display:block` in css) and **if false** then **the element is set to hidden** (so display:none).
104-
* With ngHide this is the reverse whereas true values cause the element itself to become
105-
* hidden.
160+
* The `ngHide` directive shows and hides the given HTML element conditionally based on the expression
161+
* provided to the ngHide attribute. The show and hide mechanism is a achieved by removing and adding
162+
* the `ng-hide` CSS class onto the element. The `.ng-hide` CSS class is a predefined CSS class present
163+
* in AngularJS which sets the display style to none (using an !important flag).
164+
*
165+
* <pre>
166+
* <!-- when $scope.myValue is truthy (element is hidden) -->
167+
* <div ng-hide="myValue"></div>
168+
*
169+
* <!-- when $scope.myValue is falsy (element is visible) -->
170+
* <div ng-hide="myValue" class="ng-hide"></div>
171+
* </pre>
172+
*
173+
* When the ngHide expression evaluates to true then the .ng-hide CSS class is added to the class attribute
174+
* on the element causing it to become hidden. When false, the ng-hide CSS class is removed
175+
* from the element causing the element not to appear hidden.
176+
*
177+
* ## Why is !important used?
178+
*
179+
* You may be wondering why !important is used for the .ng-hide CSS class. This is because the `.ng-hide` selector
180+
* can be easily overridden by heavier selectors. For example, something as simple
181+
* as changing the display style on a HTML list item would make hidden elements appear visible.
182+
* This also becomes a bigger issue when dealing with CSS frameworks.
183+
*
184+
* By using !important, the show and hide behavior will work as expected despite any clash between CSS selector
185+
* specificity (when !important isn't used with any conflicting styles). If a developer chooses to override the
186+
* styling to change how to hide an element then it is just a matter of using !important in their own CSS code.
187+
*
188+
* ### Overriding .ng-hide
189+
*
190+
* If you wish to change the hide behavior with ngShow/ngHide then this can be achieved by
191+
* restating the styles for the .ng-hide class in CSS:
192+
* <pre>
193+
* .ng-hide {
194+
* //!annotate CSS Specificity|Not to worry, this will override the AngularJS default...
195+
* display:block!important;
196+
*
197+
* //this is just another form of hiding an element
198+
* position:absolute;
199+
* top:-9999px;
200+
* left:-9999px;
201+
* }
202+
* </pre>
203+
*
204+
* Just remember to include the important flag so the CSS override will function.
205+
*
206+
* ## A note about animations with ngHide
207+
*
208+
* Animations in ngShow/ngHide work with the show and hide events that are triggered when the directive expression
209+
* is true and false. This system works similar to the animation system present with ngClass, however, the
210+
* only difference is that you must also include the !important flag to override the display property so
211+
* that you can perform an animation when the element is hidden during the time of the animation.
212+
*
213+
* <pre>
214+
* //
215+
* //a working example can be found at the bottom of this page
216+
* //
217+
* .my-element.ng-hide-add, .my-element.ng-hide-remove {
218+
* transition:0.5s linear all;
219+
* display:block!important;
220+
* }
221+
*
222+
* .my-element.ng-hide-add { ... }
223+
* .my-element.ng-hide-add.ng-hide-add-active { ... }
224+
* .my-element.ng-hide-remove { ... }
225+
* .my-element.ng-hide-remove.ng-hide-remove-active { ... }
226+
* </pre>
106227
*
107228
* @animations
108-
* show - happens after the ngHide expression evaluates to a non truthy value and the contents are set to visible
109-
* hide - happens after the ngHide expression evaluates to a truthy value and just before the contents are set to hidden
229+
* removeClass: .ng-hide - happens after the ngHide expression evaluates to a truthy value and just before the contents are set to hidden
230+
* addClass: .ng-hide - happens after the ngHide expression evaluates to a non truthy value and just before the contents are set to visible
110231
*
111232
* @element ANY
112233
* @param {expression} ngHide If the {@link guide/expression expression} is truthy then

0 commit comments

Comments
 (0)