Angular 4 - 显示和隐藏组件

时间:2017-10-30 10:30:30

标签: angular angular-template

我在同一个html文件中有下一个组件。

<app-form></app-form>
<app-results [hidden]="isOn"></app-results>
<app-contact-primary [hidden]="!isOn"></<app-contact-primary>
<app-contact-second [hidden]="!isOn"></app-contact-second>

在组件&#34; app-form&#34;中,我有两个按钮:

<button pButton label="Contact" (click)="">Contact</button>
<button pButton label="Results" (click)="">Results</button>

如果我点击按钮&#34;请联系&#34;必须显示组件&#34; app-contact-primary&#34;和&#34; app-contact-second&#34;并隐藏&#34; app-results&#34;成分

但如果我点击按钮&#34;结果&#34;必须隐藏组件&#34; app-contact-primary&#34;和&#34; app-contact-second&#34;并显示&#34; app-results&#34;成分

我怎么能这样做?

由于

3 个答案:

答案 0 :(得分:10)

你可以使用hidden或ngIf:

<app-form></app-form>
<app-results *ngIf="isOn"></app-results>
<app-contact-primary *ngIf="!isOn"></<app-contact-primary>
<app-contact-second *ngIf="!isOn"></app-contact-second>

<button pButton label="Contact" (click)="isOn= false">Contact</button>
<button pButton label="Results" (click)="isOn= true">Results</button>

答案 1 :(得分:1)

只需使用

public show: boolean = false;
public buttonName: any = true;

toggle() {
    this.show = !this.show;
    if(this.show)
        this.buttonName = false;
    else
        this.buttonName = true;
}

<div *ngIf="show">
<textarea #todoitem class="></textarea>
</div>
<button type="button" (click)="addItem('status')">Add</button>
<button type="button" (click)="toggle()">Close</button>
<div *ngIf="buttonName">
<a (click)="toggle()"><i class="fa fa-plus text-white"></i></a>
</div>

答案 2 :(得分:0)

只需将变量设为true和false

<button pButton label="Contact" (click)="isOn = true">Contact</button>
<button pButton label="Results" (click)="isOn = false">Results</button>