?
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
ngShow
指令根據(jù)ngShow屬性上表達(dá)式來(lái)顯示或隱藏給定的HTML元素。元素的顯示或隱藏是通過刪除或添加ng-hide
?CSS 類到元素上來(lái)實(shí)現(xiàn)的。.ng-hide?CSS 類是在AngularJS預(yù)定義的,并且設(shè)置顯示樣式為空(使用!important標(biāo)記)。 對(duì)于CSP 模式請(qǐng)?zhí)砑?angular-csp.css?到你的html文件(參見ngCsp)。
<!-- when $scope.myValue is truthy (element is visible) -->
<div ng-show="myValue"></div>
<!-- when $scope.myValue is falsy (element is hidden) -->
<div ng-show="myValue" class="ng-hide"></div>
當(dāng)ngShow表達(dá)式計(jì)算結(jié)果為假時(shí),ng-hide CSS 類會(huì)被加到元素的class屬性中,從而讓它變?yōu)殡[藏。當(dāng)為真時(shí),ng-hide CSS 類會(huì)從元素移除,這樣元素就不會(huì)被隱藏。
你可能想知道為什么要在ng-hide CSS類上使用!important。這是因?yàn)?.ng-hide選擇器很容易被級(jí)別更高的選擇器覆蓋。例如,一些簡(jiǎn)單的改變HTML列表?xiàng)l目的顯示樣式會(huì)讓隱藏的元素顯示。這成為了處理CSS框架時(shí)最大的問題。
通過使用!important,顯示和隱藏行為會(huì)按預(yù)期工作,而不管CSS選擇器間的任何沖突(當(dāng)!important沒用在任何相矛盾的樣式上時(shí))。如果開發(fā)者決定覆蓋樣式來(lái)改變?nèi)绾坞[藏元素,那只需要在自己的CSS代碼中配合使!important即可。
默認(rèn)情況下,.ng-hide
類使用 display:none!important
格式化元素。如果你希望改變ngShow/ngHide的隱藏行為,可通過在CSS中重新定義 .ng-hide
類來(lái)實(shí)現(xiàn):
.ng-hide {
/* this is just another form of hiding an element */
display:block!important;
position:absolute;
top:-9999px;
left:-9999px;}
默認(rèn)情況下你不需要覆蓋CSS中任何東西,動(dòng)畫就會(huì)針對(duì)顯示樣式生效。
ngShow/ngHide中動(dòng)畫的顯示和隱藏事件會(huì)在指令表達(dá)式為真或假時(shí)觸發(fā)。 這個(gè)系統(tǒng)工作方式像ngClass的動(dòng)畫系統(tǒng),除了你必須也包含!important標(biāo)記到覆蓋的顯示屬性中,這樣就可以在元素隱藏動(dòng)畫期間正確執(zhí)行。
////a working example can be found at the bottom of this page
//.my-element.ng-hide-add, .my-element.ng-hide-remove {
/* this is required as of 1.3x to properly
apply all styling in a show/hide animation */
transition:0s linear all;}
.my-element.ng-hide-add-active,.my-element.ng-hide-remove-active {
/* the transition is defined in the active class */
transition:1s linear all;}
.my-element.ng-hide-add { ... }.my-element.ng-hide-add.ng-hide-add-active { ... }.my-element.ng-hide-remove { ... }.my-element.ng-hide-remove.ng-hide-remove-active { ... }
記住,AngularJS的1.3.0-beta.11版本里, 不需要通過阻塞動(dòng)畫過程來(lái)改變顯示屬性,ngAnimate會(huì)為你自動(dòng)處理樣式的切換。
<ANY
ng-show="">
...
</ANY>
addClass: .ng-hide - 在ngShow表達(dá)式計(jì)算結(jié)果為非真時(shí)生效,并且是在內(nèi)容被顯示前。
removeClass: .ng-hide - 在ngShow表達(dá)式計(jì)算結(jié)果為真時(shí)生效,并且是在內(nèi)容被隱藏前。
點(diǎn)擊這里 了解更多關(guān)于涉及動(dòng)畫的步驟。參數(shù) | 類型 | 詳述 |
---|---|---|
ngShow | expression | 如果表達(dá)式為真則顯示元素,否則隱藏。 |
Click me: <input type="checkbox" ng-model="checked"><br/><div>
Show:
<div class="check-element animate-show" ng-show="checked">
<span class="glyphicon glyphicon-thumbs-up"></span> I show up when your checkbox is checked.
</div>
</div><div>
Hide:
<div class="check-element animate-show" ng-hide="checked">
<span class="glyphicon glyphicon-thumbs-down"></span> I hide when your checkbox is checked.
</div>
</div>
@import url(//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css);
.animate-show {
line-height:20px;
opacity:1;
padding:10px;
border:1px solid black;
background:white;}
.animate-show.ng-hide-add.ng-hide-add-active,.animate-show.ng-hide-remove.ng-hide-remove-active {
-webkit-transition:all linear 0.5s;
transition:all linear 0.5s;}
.animate-show.ng-hide {
line-height:0;
opacity:0;
padding:0 10px;}
.check-element {
padding:10px;
border:1px solid black;
background:white;}
var thumbsUp = element(by.css('span.glyphicon-thumbs-up'));var thumbsDown = element(by.css('span.glyphicon-thumbs-down'));
it('should check ng-show / ng-hide', Function() {
expect(thumbsUp.isDisplayed()).toBeFalsy();
expect(thumbsDown.isDisplayed()).toBeTruthy();
element(by.model('checked')).click();
expect(thumbsUp.isDisplayed()).toBeTruthy();
expect(thumbsDown.isDisplayed()).toBeFalsy();});