Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 - ngShow equivalent? [duplicate]

According to my interpretation of the documentation, if I want to be able to have an element hidden by default, and shown when a link is clicked, the following ought to work?

  1. In /app/app.component.ts

    newTrustFormVisible: false;
    
  2. In /app/app.component.html

    <a href="#" (click)="newTrustFormVisible = !newTrustFormVisible;">[Add New]</a>
    
    <div ng-show="newTrustFormVisible" class="panel panel-default">
      ...
    </div>
    

However, this does not work. It also produces no errors. What am I missing?

like image 448
Max Griffin Avatar asked Feb 14 '17 01:02

Max Griffin


People also ask

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

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

What is ngShow in angular?

The ng-show Directive in AngluarJS is used to show or hide the specified HTML element. If the given expression in the ng-show attribute is true then the HTML element will display otherwise it hides the HTML element. It is supported by all HTML elements.

What is the angular equivalent to an AngularJS watch?

You can use getter function or get accessor to act as watch on angular 2.

What is the difference between ngIf and hidden?

The main difference is that *ngIf will remove the element from the DOM, while [hidden] actually plays with the CSS style by setting display:none . Generally it is expensive to add and remove stuff from the DOM for frequent actions.


1 Answers

Your using Angular 1 directives. For Angular 2 use *ngIf for components that do not need to be in the DOM when they are hidden or bind to the HTML hidden property [hidden] if you want the component to always be in the DOM but hidden with CSS.

e.g:

<div *ngIf="newTrustFormVisible" class="panel panel-default">

or

<div [hidden]="!newTrustFormVisible" class="panel panel-default">

Angular 1 to Angular 2 reference

*ngIf

like image 66
shusson Avatar answered Oct 19 '22 03:10

shusson