What is the equivalent of ngShow and ngHide in Angular 2+?

后端 未结 18 2206
离开以前
离开以前 2020-11-22 14:46

I have a number of elements that I want to be visible under certain conditions.

In AngularJS I would write

stuff
相关标签:
18条回答
  • 2020-11-22 15:24

    According to Angular 1 documentation of ngShow and ngHide, both of these directive adds the css style display: none !important;, to the element according to the condition of that directive (for ngShow adds the css on false value, and for ngHide adds the css for true value).

    We can achieve this behavior using Angular 2 directive ngClass:

    /* style.css */
    .hide 
    {
        display: none !important;
    }
    
    <!-- old angular1 ngShow -->
    <div ng-show="ngShowVal"> I'm Angular1 ngShow... </div>
    
    <!-- become new angular2 ngClass -->
    <div [ngClass]="{ 'hide': !ngShowVal }"> I'm Angular2 ngShow... </div>
    
    <!-- old angular2 ngHide -->
    <div ng-hide="ngHideVal"> I'm Angular1 ngHide... </div>
    
    <!-- become new angular2 ngClass -->
    <div [ngClass]="{ 'hide': ngHideVal }"> I'm Angular2 ngHide... </div>
    

    Notice that for show behavior in Angular2 we need to add ! (not) before the ngShowVal, and for hide behavior in Angular2 we don't need to add ! (not) before the ngHideVal.

    0 讨论(0)
  • 2020-11-22 15:27

    Just bind to the hidden property

    [hidden]="!myVar"
    

    See also

    • https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/hidden

    issues

    hidden has some issues though because it can conflict with CSS for the display property.

    See how some in Plunker example doesn't get hidden because it has a style

    :host {display: block;}
    

    set. (This might behave differently in other browsers - I tested with Chrome 50)

    workaround

    You can fix it by adding

    [hidden] { display: none !important;}
    

    To a global style in index.html.

    another pitfall

    hidden="false"
    hidden="{{false}}"
    hidden="{{isHidden}}" // isHidden = false;
    

    are the same as

    hidden="true"
    

    and will not show the element.

    hidden="false" will assign the string "false" which is considered truthy.
    Only the value false or removing the attribute will actually make the element visible.

    Using {{}} also converts the expression to a string and won't work as expected.

    Only binding with [] will work as expected because this false is assigned as false instead of "false".

    *ngIf vs [hidden]

    *ngIf effectively removes its content from the DOM while [hidden] modifies the display property and only instructs the browser to not show the content but the DOM still contains it.

    0 讨论(0)
  • 2020-11-22 15:28

    Use the [hidden] attribute:

    [hidden]="!myVar"
    

    Or you can use *ngIf

    *ngIf="myVar"
    

    These are two ways to show/hide an element. The only difference is: *ngIf will remove the element from DOM while [hidden] will tell the browser to show/hide an element using CSS display property by keeping the element in DOM.

    0 讨论(0)
  • 2020-11-22 15:28
    <div [hidden]="flagValue">
    ---content---
    </div>
    
    0 讨论(0)
  • 2020-11-22 15:34

    Use hidden like you bind any model with control and specify css for it:

    HTML:

    <input type="button" class="view form-control" value="View" [hidden]="true" />
    

    CSS:

    [hidden] {
       display: none;
    }
    
    0 讨论(0)
  • 2020-11-22 15:35

    I find myself in the same situation with the difference than in my case the element was a flex container.If is not your case an easy work around could be

    [style.display]="!isLoading ? 'block' : 'none'"
    

    in my case due to the fact that a lot of browsers that we support still need the vendor prefix to avoid problems i went for another easy solution

    [class.is-loading]="isLoading"
    

    where then the CSS is simple as

    &.is-loading { display: none } 
    

    to leave then the displayed state handled by the default class.

    0 讨论(0)
提交回复
热议问题