angular - 是否可以使用 ng-content 将 mat-options 传递给我的自定义 mat-select 组件?

标签 angular typescript angular-material

我有一个自定义选择组件,想使用 ng-content 将我的选项传递给它,如下所示:

<lib-select [(selected)]="selected" (selectedChange)="onChange($event)">
            <mat-option [value]="0">Value 1</mat-option>
            <mat-option [value]="1">Value 2</mat-option>
            <mat-option [value]="2">Value 3</mat-option>
            <mat-option [value]="3">Value 4</mat-option>
            <mat-option [value]="4">Value 5</mat-option>
</lib-select>

虽然这似乎不起作用。一开始它甚至没有显示选项。我找到了让它们显示的技巧,但我仍然无法选择任何内容。这是我的组件:

    <mat-select panelClass="select" disableRipple (selectionChange)="onChange()" [(value)]="selected" disableOptionCentering>
        <mat-select-trigger>{{selected}}</mat-select-trigger>
        <!-- mat-option below is required to render ng-content in mat-select. this is an ugly hack and there might be a better workaround for this -->
        <mat-option [value]="" style="display: none;"></mat-option>
        <ng-content></ng-content>
    </mat-select>

有什么方法可以让它工作,还是 mat-select 不能与 ng-content 一起工作?

我知道我可以使用 @Input() 将选项传递到组件中,但我认为使用 ng-content 时代码看起来更清晰。

编辑:看起来我实际上可以选择项目。问题是我可以选择多个选项并且有链式 react ,即使 disableRipple 存在于我的 mat-select 上。

最佳答案

有一个解决方法。将 ng-content 放在隐藏的 div 中,并创建询问 ContentChildren(MatOption) 的选项,参见 stackblitz 中的示例

组件是

import {Component, ContentChildren, AfterViewInit, QueryList} from "@angular/core";
import { MatOption } from "@angular/material/core";

@Component({
  selector: "custom-select",
  template: `
    <mat-form-field>
      <mat-label>Favorite food</mat-label>
      <mat-select>
        <ng-container *ngIf="yet">
          <mat-option *ngFor="let option of options" [value]="option.value">
            {{ option.viewValue }}
          </mat-option>
        </ng-container>
      </mat-select>
    </mat-form-field>
    <div style="display:none" *ngIf="!yet">
      <ng-content></ng-content>
    </div>
  `
})
export class CustomSelect implements AfterViewInit {
  @ContentChildren(MatOption) queryOptions: QueryList<MatOption>;
  options: any[];
  yet: boolean;
  ngAfterViewInit() {
    this.options = this.queryOptions.map(x => {
      return { value: x.value, viewValue: x.viewValue };
    });
    setTimeout(() => {
      this.yet = true;
    });
  }
}

使用

<custom-select>
    <mat-option *ngFor="let food of foods" [value]="food.value">
      {{food.viewValue}}
    </mat-option>
</custom-select>

关于angular - 是否可以使用 ng-content 将 mat-options 传递给我的自定义 mat-select 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58487427/

相关文章:

mysql - NestJS typeorm - 无法创建实体

angular - 如何将 Angular Material 日期选择器中的日期作为字符串绑定(bind)到模型?

javascript - 每当对象内容发生更改时,Angular 就会触发 EventEmitter

Angular 2 管理导航/路由与客户端导航/路由分开

TypeScript 迁移 : Simplest fix for JavaScript style const definitions generating errors?

javascript - 让 TypeScript 明白对象属性不能被取消定义?

angular - 垫子工具栏对齐中的 Material 图标中的文本

angular - 如何异步订阅用于candeactivate Guard的matdialog服务?

javascript - Angular2动态改变CSS属性

如果没有数据模型,则 Angular Material 在列表之间拖放