目录 搜索
AngularJS API Reference auto auto/service auto/service/$injector auto/service/$provide ng ng/directive ng/directive/a ng/directive/form ng/directive/input ng/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/ngApp ng/directive/ngBind ng/directive/ngBindHtml ng/directive/ngBindTemplate ng/directive/ngBlur ng/directive/ngChange ng/directive/ngChecked ng/directive/ngClass ng/directive/ngClassEven ng/directive/ngClassOdd ng/directive/ngClick ng/directive/ngCloak ng/directive/ngController ng/directive/ngCopy ng/directive/ngCsp ng/directive/ngCut ng/directive/ngDblclick ng/directive/ngDisabled ng/directive/ngFocus ng/directive/ngForm ng/directive/ngHide ng/directive/ngHref ng/directive/ngIf ng/directive/ngInclude ng/directive/ngInit ng/directive/ngKeydown ng/directive/ngKeypress ng/directive/ngKeyup ng/directive/ngList ng/directive/ngModel ng/directive/ngModelOptions ng/directive/ngMousedown ng/directive/ngMouseenter ng/directive/ngMouseleave ng/directive/ngMousemove ng/directive/ngMouseover ng/directive/ngMouseup ng/directive/ngNonBindable ng/directive/ngOpen ng/directive/ngPaste ng/directive/ngPluralize ng/directive/ngReadonly ng/directive/ngRepeat ng/directive/ngSelected ng/directive/ngShow ng/directive/ngSrc ng/directive/ngSrcset ng/directive/ngStyle ng/directive/ngSubmit ng/directive/ngSwitch ng/directive/ngTransclude ng/directive/ngValue ng/directive/script ng/directive/select ng/directive/textarea ng/filter ng/filter/currency ng/filter/date ng/filter/filter ng/filter/json ng/filter/limitTo ng/filter/lowercase ng/filter/number ng/filter/orderBy ng/filter/uppercase ng/function ng/function/angular.bind ng/function/angular.bootstrap ng/function/angular.copy ng/function/angular.element ng/function/angular.equals ng/function/angular.extend ng/function/angular.forEach ng/function/angular.fromJson ng/function/angular.identity ng/function/angular.injector ng/function/angular.isArray ng/function/angular.isDate ng/function/angular.isDefined ng/function/angular.isElement ng/function/angular.isFunction ng/function/angular.isNumber ng/function/angular.isObject ng/function/angular.isString ng/function/angular.isUndefined ng/function/angular.lowercase ng/function/angular.module ng/function/angular.noop ng/function/angular.toJson ng/function/angular.uppercase ng/object ng/object/angular.version ng/provider ng/provider/$animateProvider ng/provider/$compileProvider ng/provider/$controllerProvider ng/provider/$filterProvider ng/provider/$httpProvider ng/provider/$interpolateProvider ng/provider/$locationProvider ng/provider/$logProvider ng/provider/$parseProvider ng/provider/$rootScopeProvider ng/provider/$sceDelegateProvider ng/provider/$sceProvider ng/service ng/service/$anchorScroll ng/service/$animate ng/service/$cacheFactory ng/service/$compile ng/service/$controller ng/service/$document ng/service/$exceptionHandler ng/service/$filter ng/service/$http ng/service/$httpBackend ng/service/$interpolate ng/service/$interval ng/service/$locale ng/service/$location ng/service/$log ng/service/$parse ng/service/$q ng/service/$rootElement ng/service/$rootScope ng/service/$sce ng/service/$sceDelegate ng/service/$templateCache ng/service/$timeout ng/service/$window ng/type ng/type/$cacheFactory.Cache ng/type/$compile.directive.Attributes ng/type/$rootScope.Scope ng/type/angular.Module ng/type/form.FormController ng/type/ngModel.NgModelController ngAnimate ngAnimate/provider ngAnimate/provider/$animateProvider ngAnimate/service ngAnimate/service/$animate ngCookies ngCookies/service ngCookies/service/$cookies ngCookies/service/$cookieStore ngMessages ngMessages/directive ngMessages/directive/ngMessage ngMessages/directive/ngMessages ngMock ngMock/function ngMock/function/angular.mock.dump ngMock/function/angular.mock.inject ngMock/function/angular.mock.module ngMock/object ngMock/object/angular.mock ngMock/provider ngMock/provider/$exceptionHandlerProvider ngMock/service ngMock/service/$exceptionHandler ngMock/service/$httpBackend ngMock/service/$interval ngMock/service/$log ngMock/service/$timeout ngMock/type ngMock/type/angular.mock.TzDate ngMockE2E ngMockE2E/service ngMockE2E/service/$httpBackend ngResource ngResource/service ngResource/service/$resource ngRoute ngRoute/directive ngRoute/directive/ngView ngRoute/provider ngRoute/provider/$routeProvider ngRoute/service ngRoute/service/$route ngRoute/service/$routeParams ngSanitize ngSanitize/filter ngSanitize/filter/linky ngSanitize/service ngSanitize/service/$sanitize ngTouch ngTouch/directive ngTouch/directive/ngClick ngTouch/directive/ngSwipeLeft ngTouch/directive/ngSwipeRight ngTouch/service ngTouch/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();});
上一篇: 下一篇: