angular - 在 mat-select Angular 中设置文本

标签 angular angular-material

我有一个像这样的 mat-select 字段:

<mat-form-field>
    <mat-select (click)="onSelectOpen()" placeholder="Store" (selectionChange)="setStoreTitle($event)" title="Store" formControlName="store" id="store" name="store">
        <mat-option *ngFor="let store of stores" [value]="store.Title">
            {{store.Title}}
            <span class="store-address">{{store.Address}}</span>
        </mat-option>
    </mat-select>
</mat-form-field>

当我尝试访问该值时,我得到了商店标题,这很好,但在下拉列表中它还显示了代码中写入的跨度。如何隐藏选择框中的跨度文本?

最佳答案

从您的问题来看,这不是很清楚,但从您的评论来看,您似乎正在寻找的是在 mat-option 中设置可用的 span 标签> 但一旦选择它就不应该出现。您要找的是MatSelectTrigger 。根据文档

MatSelectTrigger allows the user to customize the trigger that is displayed when the select has a value.

因此您需要做的就是在 mat-select-trigger 中显示 formControl

<mat-form-field>
    <mat-select (click)="onSelectOpen()" placeholder="Store" (selectionChange)="setStoreTitle($event)" title="Store" formControlName="store" id="store" name="store">
        <mat-select-trigger>
            {{form.get('store').value}} <!-- where form is the name of your FormGroup -->
        </mat-select-trigger>
        <mat-option *ngFor="let store of stores" [value]="store.Title">
            {{store.Title}}
            <span class="store-address">{{store.Address}}</span>
        </mat-option>
    </mat-select>
</mat-form-field>

关于angular - 在 mat-select Angular 中设置文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57653653/

相关文章:

angular - 获取项目在 cdkDropList 中的放置位置

Angular 2 - 具有 ChangeDetectorRef 依赖性的测试组件

javascript - 来自组件的 Angular 事件监听器

javascript - 如何在 Angular 6 中添加图像而不清除前一个图像

javascript - 在 md-select 中使用 md-virtual-repeat 不起作用?

angular - 如何将 Angular Material 扩展面板箭头图标定位在左侧

Angular Material 日期选择器限制范围选择

angular - Angular Material ErrorStateMatcher 可以检测父表单何时提交吗?

angular - Angular 组件 ExpressionChangedAfterItHasBeenCheckedError 中的 Mat 对话框错误

Angular - 通过条件逻辑在不同位置显示投影内容