目录搜索
AngularJS API Referenceautoauto/serviceauto/service/$injectorauto/service/$providengng/directiveng/directive/ang/directive/formng/directive/inputng/directive/input[checkbox]ng/directive/input[date]ng/directive/input[dateTimeLocal]ng/directive/input[email]ng/directive/input[month]ng/directive/input[number]ng/directive/input[radio]ng/directive/input[text]ng/directive/input[time]ng/directive/input[url]ng/directive/input[week]ng/directive/ngAppng/directive/ngBindng/directive/ngBindHtmlng/directive/ngBindTemplateng/directive/ngBlurng/directive/ngChangeng/directive/ngCheckedng/directive/ngClassng/directive/ngClassEvenng/directive/ngClassOddng/directive/ngClickng/directive/ngCloakng/directive/ngControllerng/directive/ngCopyng/directive/ngCspng/directive/ngCutng/directive/ngDblclickng/directive/ngDisabledng/directive/ngFocusng/directive/ngFormng/directive/ngHideng/directive/ngHrefng/directive/ngIfng/directive/ngIncludeng/directive/ngInitng/directive/ngKeydownng/directive/ngKeypressng/directive/ngKeyupng/directive/ngListng/directive/ngModelng/directive/ngModelOptionsng/directive/ngMousedownng/directive/ngMouseenterng/directive/ngMouseleaveng/directive/ngMousemoveng/directive/ngMouseoverng/directive/ngMouseupng/directive/ngNonBindableng/directive/ngOpenng/directive/ngPasteng/directive/ngPluralizeng/directive/ngReadonlyng/directive/ngRepeatng/directive/ngSelectedng/directive/ngShowng/directive/ngSrcng/directive/ngSrcsetng/directive/ngStyleng/directive/ngSubmitng/directive/ngSwitchng/directive/ngTranscludeng/directive/ngValueng/directive/scriptng/directive/selectng/directive/textareang/filterng/filter/currencyng/filter/dateng/filter/filterng/filter/jsonng/filter/limitTong/filter/lowercaseng/filter/numberng/filter/orderByng/filter/uppercaseng/functionng/function/angular.bindng/function/angular.bootstrapng/function/angular.copyng/function/angular.elementng/function/angular.equalsng/function/angular.extendng/function/angular.forEachng/function/angular.fromJsonng/function/angular.identityng/function/angular.injectorng/function/angular.isArrayng/function/angular.isDateng/function/angular.isDefinedng/function/angular.isElementng/function/angular.isFunctionng/function/angular.isNumberng/function/angular.isObjectng/function/angular.isStringng/function/angular.isUndefinedng/function/angular.lowercaseng/function/angular.moduleng/function/angular.noopng/function/angular.toJsonng/function/angular.uppercaseng/objectng/object/angular.versionng/providerng/provider/$animateProviderng/provider/$compileProviderng/provider/$controllerProviderng/provider/$filterProviderng/provider/$httpProviderng/provider/$interpolateProviderng/provider/$locationProviderng/provider/$logProviderng/provider/$parseProviderng/provider/$rootScopeProviderng/provider/$sceDelegateProviderng/provider/$sceProviderng/serviceng/service/$anchorScrollng/service/$animateng/service/$cacheFactoryng/service/$compileng/service/$controllerng/service/$documentng/service/$exceptionHandlerng/service/$filterng/service/$httpng/service/$httpBackendng/service/$interpolateng/service/$intervalng/service/$localeng/service/$locationng/service/$logng/service/$parseng/service/$qng/service/$rootElementng/service/$rootScopeng/service/$sceng/service/$sceDelegateng/service/$templateCacheng/service/$timeoutng/service/$windowng/typeng/type/$cacheFactory.Cacheng/type/$compile.directive.Attributesng/type/$rootScope.Scopeng/type/angular.Moduleng/type/form.FormControllerng/type/ngModel.NgModelControllerngAnimatengAnimate/providerngAnimate/provider/$animateProviderngAnimate/servicengAnimate/service/$animatengCookiesngCookies/servicengCookies/service/$cookiesngCookies/service/$cookieStorengMessagesngMessages/directivengMessages/directive/ngMessagengMessages/directive/ngMessagesngMockngMock/functionngMock/function/angular.mock.dumpngMock/function/angular.mock.injectngMock/function/angular.mock.modulengMock/objectngMock/object/angular.mockngMock/providerngMock/provider/$exceptionHandlerProviderngMock/servicengMock/service/$exceptionHandlerngMock/service/$httpBackendngMock/service/$intervalngMock/service/$logngMock/service/$timeoutngMock/typengMock/type/angular.mock.TzDatengMockE2EngMockE2E/servicengMockE2E/service/$httpBackendngResourcengResource/servicengResource/service/$resourcengRoutengRoute/directivengRoute/directive/ngViewngRoute/providerngRoute/provider/$routeProviderngRoute/servicengRoute/service/$routengRoute/service/$routeParamsngSanitizengSanitize/filterngSanitize/filter/linkyngSanitize/servicengSanitize/service/$sanitizengTouchngTouch/directivengTouch/directive/ngClickngTouch/directive/ngSwipeLeftngTouch/directive/ngSwipeRightngTouch/servicengTouch/service/$swipe
文字

AngularJS: API: ng/directive/ngShow

ngShow

  1. - directive in module ng

ngShow指令根据ngShow属性上表达式来显示或隐藏给定的HTML元素。元素的显示或隐藏是通过删除或添加ng-hide CSS 类到元素上来实现的。.ng-hide CSS 类是在AngularJS预定义的,并且设置显示样式为空(使用!important标记)。 对于CSP 模式请添加 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>

当ngShow表达式计算结果为假时,ng-hide CSS 类会被加到元素的class属性中,从而让它变为隐藏。当为真时,ng-hide CSS 类会从元素移除,这样元素就不会被隐藏。

为什么使用!important?

你可能想知道为什么要在ng-hide CSS类上使用!important。这是因为 .ng-hide选择器很容易被级别更高的选择器覆盖。例如,一些简单的改变HTML列表条目的显示样式会让隐藏的元素显示。这成为了处理CSS框架时最大的问题。

通过使用!important,显示和隐藏行为会按预期工作,而不管CSS选择器间的任何冲突(当!important没用在任何相矛盾的样式上时)。如果开发者决定覆盖样式来改变如何隐藏元素,那只需要在自己的CSS代码中配合使!important即可。

覆盖 .ng-hide

默认情况下,.ng-hide类使用 display:none!important格式化元素。如果你希望改变ngShow/ngHide的隐藏行为,可通过在CSS中重新定义 .ng-hide类来实现:

.ng-hide {
  /* this is just another form of hiding an element */
  display:block!important;
  position:absolute;
  top:-9999px;
  left:-9999px;}

默认情况下你不需要覆盖CSS中任何东西,动画就会针对显示样式生效。

ngShow动画的一些注意点

ngShow/ngHide中动画的显示和隐藏事件会在指令表达式为真或假时触发。 这个系统工作方式像ngClass的动画系统,除了你必须也包含!important标记到覆盖的显示属性中,这样就可以在元素隐藏动画期间正确执行。

////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版本里, 不需要通过阻塞动画过程来改变显示属性,ngAnimate会为你自动处理样式的切换。

指令信息

  • 这个指令执行优先级为0.

用法

  • 作为属性使用:
    <ANY
      ng-show="">
    ...
    </ANY>

动画

addClass: .ng-hide - 在ngShow表达式计算结果为非真时生效,并且是在内容被显示前。

removeClass: .ng-hide - 在ngShow表达式计算结果为真时生效,并且是在内容被隐藏前。

点击这里 了解更多关于涉及动画的步骤。

参数

参数 类型 详述
ngShow expression

如果表达式为真则显示元素,否则隐藏。

示例

index.html
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>
glyphicons.css
@import url(//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css);
animations.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;}
protractor.js
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();});
上一篇:下一篇: