且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Angular 2 - 相当于ngShow?

更新时间:2022-10-15 20:44:02

您使用Angular 1指令。对于Angular 2,当组件隐藏或绑定到HTML隐藏属性 [hidden]时,不需要在DOM中使用 * ngIf 如果您希望组件始终位于DOM中,但隐藏了CSS。



例如:

 < div * ngIf =newTrustFormVisibleclass =panel panel-default> 

 < div [hidden] =!newTrustFormVisibleclass =panel panel-default> 

Angular 1 to Angular 2参考资料

* ngIf


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?

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