Javascript Angular 2 - ngShow 等价物?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42216265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular 2 - ngShow equivalent?
提问by Max Griffin
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?
根据我对文档的解释,如果我希望能够在默认情况下隐藏一个元素,并在单击链接时显示,以下应该有效吗?
In /app/app.component.ts
newTrustFormVisible: false;In /app/app.component.html
<a href="#" (click)="newTrustFormVisible = !newTrustFormVisible;">[Add New]</a> <div ng-show="newTrustFormVisible" class="panel panel-default"> ... </div>
在 /app/app.component.ts
newTrustFormVisible: false;在 /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?
但是,这不起作用。它也不会产生错误。我错过了什么?
回答by shusson
Your using Angular 1 directives. For Angular 2 use *ngIffor 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.
您使用的 Angular 1 指令。对于 Angular 2 *ngIf,[hidden]如果您希望组件始终在 DOM 中但使用 CSS 隐藏,则将它们用于隐藏时不需要在 DOM 中或绑定到 HTML 隐藏属性的组件。
e.g:
例如:
<div *ngIf="newTrustFormVisible" class="panel panel-default">
or
或者
<div [hidden]="!newTrustFormVisible" class="panel panel-default">

