【问题标题】:Angular - Remove label in mat-tabAngular - 删除 mat-tab 中的标签
【发布时间】:2018-03-06 15:19:14
【问题描述】:

我有一个由两个选项卡组成的表单。
根据 *ngIf,我决定要显示什么样的选项卡。
- 如果我的表单是视图,我希望看到两个带有标签的选项卡(两个选项卡都有向用户显示的信息,第二个选项卡是结果)。
- 如果我的表单是一个添加,我不想看到任何标签,因此没有标签。

我不愿意创建两个单独的文件,因为我试图尽可能减少我的代码。

我尝试添加一个替换 mat-tab-label 的 ng-template,但我的标签是空的,而不是删除它。

到目前为止,这就是我所拥有的。我的问题是:是否可以在不创建新文件的情况下删除整个标签标签,如果可以,我该怎么做?

<mat-dialog-content class="ManageTask">
<form [formGroup]="form">
    <mat-tab-group mat-stretch-tabs>
        <mat-tab>
            <ng-template *ngIf="isView()" mat-tab-label>Task details</ng-template>
            <div class="left">
                <mat-form-field class="full-width">
                    <input matInput type="text" placeholder="Name" formControlName="name">
                    <mat-error *ngIf="form.controls['name'].hasError('required')">Name required</mat-error>
                </mat-form-field>
                <br/>
                <mat-form-field class="full-width">
                    <textarea matInput type="text" placeholder="Description" formControlName="description"></textarea>
                    <mat-error *ngIf="form.controls['description'].hasError('required')">Description required</mat-error>
                </mat-form-field>
            </div>
        </mat-tab>
        <mat-tab *ngIf="isView()" label="Task results">
            <div>...MORE CONTENT...</div>
        </mat-tab>
    </mat-tab-group>
</form>

【问题讨论】:

    标签: html angular


    【解决方案1】:

    我不确定我是否理解这个问题。当然你可以做到。像这样使用&lt;ng-template&gt;andngIf 的正确方法是:

    <ng-template [ngIf]="isView()">
        <mat-tab label="Task details">...</mat-tab>
    </ng-template>
    

    或:

    &lt;mat-tab *ngIf="isView()" label="Task details"&gt;...&lt;/mat-tab&gt;

    [ngif]*ngif:Angular Structural Directives 之间的区别

    希望对你有帮助

    【讨论】:

    • 我重新使用了您对 ng-templates 的想法,我目前正在尝试使用 ng-container 修复它。我会及时通知你。
    猜你喜欢
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    • 2022-10-16
    • 1970-01-01
    • 2019-03-01
    • 2019-02-25
    • 1970-01-01
    • 2019-06-23
    相关资源
    最近更新 更多