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

后端 未结 18 2207
离开以前
离开以前 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:35

    If your case is that the style is display none you can also use the ngStyle directive and modify the display directly, I did that for a bootstrap DropDown the UL on it is set to display none.

    So I created a click event for "manually" toggling the UL to display

    <div class="dropdown">
        <button class="btn btn-default" (click)="manualtoggle()"  id="dropdownMenu1" >
        Seleccione una Ubicación
        <span class="caret"></span>
        </button>
        <ul class="dropdown-menu" [ngStyle]="{display:displayddl}">
            <li *ngFor="let object of Array" (click)="selectLocation(location)">{{object.Value}}</li>                                
         </ul>
     </div>    
    

    Then on the component I have showDropDown:bool attribute that I toggle every time, and based on int, set the displayDDL for the style as follows

    showDropDown:boolean;
    displayddl:string;
    manualtoggle(){
        this.showDropDown = !this.showDropDown;
        this.displayddl = this.showDropDown ? "inline" : "none";
    }
    
    0 讨论(0)
  • 2020-11-22 15:36
    <div [hidden]="myExpression">
    

    myExpression may be set to true or false

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

    Best way to deal with this issue using ngIf Because this well prevent getting that element render in front-end,

    If you use [hidden]="true" or style hide [style.display] it will only hide element in front end and someone can change the value and visible it easily, In my opinion best way to hide element is ngIf

    <div *ngIf="myVar">stuff</div>
    

    and also If you have multiple element (need to implement else also) you can Use <ng-template> option

    <ng-container *ngIf="myVar; then loadAdmin else loadMenu"></ng-container>
    <ng-template #loadMenu>
         <div>loadMenu</div>
    </ng-template>
    
    <ng-template #loadAdmin>
         <div>loadAdmin</div>
    </ng-template>  
    

    sample ng-template code

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

    To hide and show div on button click in angular 6.

    Html Code

    <button (click)=" isShow=!isShow">FormatCell</button>
    <div class="ruleOptionsPanel" *ngIf=" isShow">
    <table>
    <tr>
    <td>Name</td>
    <td>Ram</td>
    </tr>
    </table>
    </div>
    

    Component .ts Code

    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent{
     isShow=false;
      }
    

    this works for me and it is way to replace ng-hide and ng-show in angular6.

    enjoy...

    Thanks

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

    for me, [hidden]=!var has never worked.

    So, <div *ngIf="expression" style="display:none;">

    And, <div *ngIf="expression"> Always give correct results.

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

    There are two examples on Angular documents https://angular.io/guide/structural-directives#why-remove-rather-than-hide

    A directive could hide the unwanted paragraph instead by setting its display style to none.

    <p [style.display]="'block'">
      Expression sets display to "block".
      This paragraph is visible.
    </p>
    
    <p [style.display]="'none'">
      Expression sets display to "none".
      This paragraph is hidden but still in the DOM.
    </p>
    

    You can use [style.display]="'block'" to replace ngShow and [style.display]="'none'" to replace ngHide.

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