【问题标题】:How to hide certain characters from selected option's label in angular 5?如何在角度 5 中隐藏所选选项标签中的某些字符?
【发布时间】:2018-04-05 08:11:12
【问题描述】:

我在这样的 angular 5 框架中使用 angular material select。

<mat-form-field>
   <mat-select (change)="viewdropdownchange($event.target.value)" [(ngModel)]='this.current_view_id'>

<mat-option *ngFor="let viewname of secondMenu;" value={{viewname.id}}>  
 <span>{{viewname.value}}</span>
  <mat-chip-list style="float: right;margin-top: 7px">
  <mat-chip style="background-color: #3f51b5;color: #fff;">Edit </mat-chip>
  <mat-chip  style=" background-color: #ff4081;color: #fff;">Clone</mat-chip>
  <mat-chip     style="background-color: #f44336;color: #fff;">Delete</mat-chip>
</mat-chip-list>


</mat-option>

   </mat-select>

</mat-form-field> 

但所选选项的标签包含芯片标题,如编辑、克隆删除等。我想在选择框中显示为选定选项时删除它们。

如何删除剩余的编辑、删除、克隆?

【问题讨论】:

  • 我认为这里不会有干净的解决方案。您可以尝试在 dom 中查询选择适当的元素并将另一个文本分配给标签元素

标签: css angular material-design angular-material


【解决方案1】:

可以在mat-select中添加一个引用变量->#select,如果面板没有打开*ngIf="select.panelOpen"则隐藏mat-chip-list

<mat-select placeholder="Favorite food" #select>
    <mat-option *ngFor="let food of foods" [value]="food.value">
        {{ food.viewValue }}
        <mat-chip-list style="float: right;margin-top: 7px" *ngIf="select.panelOpen">
            <mat-chip style="background-color: #3f51b5;color: #fff;">Edit </mat-chip>
            <mat-chip style=" background-color: #ff4081;color: #fff;">Clone</mat-chip>
            <mat-chip style="background-color: #f44336;color: #fff;">Delete</mat-chip>
        </mat-chip-list>
    </mat-option>
</mat-select>

更新 更好的解决方案是使用MatSelectTrigger

<mat-form-field>
<mat-select placeholder="Favorite food" [formControl]="foodSelect">
    <mat-select-trigger>
        {{ foodSelect.value }}
    </mat-select-trigger>
    <mat-option *ngFor="let food of foods" [value]="food">
        {{ food }}
        <mat-chip-list style="float: right;margin-top: 7px">
            <mat-chip style="background-color: #3f51b5;color: #fff;">Edit </mat-chip>
            <mat-chip style=" background-color: #ff4081;color: #fff;">Clone</mat-chip>
            <mat-chip style="background-color: #f44336;color: #fff;">Delete</mat-chip>
        </mat-chip-list>
    </mat-option>
</mat-select>

以及组件代码。

foodSelect = new FormControl();
foods = ['Steak', 'Pizza', 'Tacos'];

这是一个更新的示例https://stackblitz.com/edit/angular-xip1me

【讨论】:

  • 嘿,它就像一个魅力,我根本不知道 panelOpen。感谢您介绍这个概念
  • 但是我得到的唯一问题是,当我在已经选择了某些内容后再次单击选择框时,编辑、克隆、删除等会闪烁,因为当时 select.panelOpen 是真的我已将选项列表定位在选择字段下方。 .
  • @Jaydeep 检查更新的答案。我认为这是一个更好的解决方案
猜你喜欢
  • 2019-02-05
  • 1970-01-01
  • 2015-10-20
  • 2023-03-17
  • 2015-01-16
  • 2011-09-08
  • 2016-01-12
  • 2014-09-23
  • 1970-01-01
相关资源
最近更新 更多