【问题标题】:How to write a custom data attribute for a 'mat-select' element in Angular Material 9如何在 Angular Material 9 中为“mat-select”元素编写自定义数据属性
【发布时间】:2020-06-05 05:19:27
【问题描述】:

我正在尝试为 Angular Material 9 中的 mat-select 元素设置自定义数据属性 (data-tag)。以下是我正在使用的 HTMLTS 代码,但没有任何反应通过在浏览器中检查网页代码:

HTML:

<mat-form-field>
    <mat-label>Course</mat-label>
        <mat-select [formControl]="subjectControl" required>
            <mat-option>-- None --</mat-option>
            <mat-optgroup *ngFor="let course of subjects" [label]="course.semester" [disabled]="course.disabled">
                <mat-option *ngFor="let subject of course.courses" [value]="subject.subjectName" [attr.data-tag]="subject.subjectSemester">
                    {{ subject.subjectName }}
                </mat-option>
            </mat-optgroup>
        </mat-select>
</mat-form-field>

TS:

export interface SubjectGroup {
    disabled?: boolean;
    semester: string;
    courses: Subject[];
}
export interface Subject {
    subjectName: string;
    subjectSemester: string;
}

export class FormComponent implements OnInit {
    subjectControl = new FormControl("", Validators.required);
    subjects: SubjectGroup[] = [
        {
            semester: "Semester 1",
            courses: [
                {
                    subjectName: "Course 1",
                    subjectSemester: "1° Semester"
                },
                {
                    subjectName: "Course 2",
                    subjectSemester: "1° Semester"
                },
                {
                    subjectName: "Course 3",
                    subjectSemester: "1° Semester"
                },
                {
                    subjectName: "Course 4",
                    subjectSemester: "1° Semester"
                },
                {
                    subjectName: "Course 5",
                    subjectSemester: "1° Semester"
                }
            ]
        },
        {
            semester: "Semester 2",
            courses: [
                {
                    subjectName: "Course 1",
                    subjectSemester: "2° Semester"
                },
                {
                    subjectName: "Course 2",
                    subjectSemester: "2° Semester"
                },
                {
                    subjectName: "Course 3",
                    subjectSemester: "2° Semester"
                },
                {
                    subjectName: "Course 4",
                    subjectSemester: "2° Semester"
                }
            ]
        }
    ];
}

我找到了this question,但我不明白我做错了什么。谢谢

#编辑 1:

我也试过了:

<mat-option *ngFor="let subject of course.courses" [value]="subject.subjectName" [attr.data-tag]="subject ? subject.subjectSemester : null">

什么都没有……

【问题讨论】:

    标签: select multidimensional-array angular-material custom-data-attribute angular-ngfor


    【解决方案1】:

    终于解决了

    HTML:

    <mat-form-field>
        <mat-label>Course</mat-label>
            <mat-select
                [formControl]="subjectControl"
                [attr.data-tag]="this.subjectControl.value"
                required
            >
                <mat-option>-- None --</mat-option>
                <mat-optgroup *ngFor="let course of subjects" [label]="course.semester" [disabled]="course.disabled">
                    <mat-option *ngFor="let subject of course.courses" [value]="subject.subjectName"><!--here I have to set [value] to `.subjectName` or `.subjectSemester` to show it into the `data-tag`-->
                        {{ subject.subjectName }}
                    </mat-option>
                </mat-optgroup>
            </mat-select>
    </mat-form-field>
    

    正如代码的cmets中所写,我必须将[attr.data-tag]放入mat-select等于this.subjectControl.value,并将mat-option[value]设置为等于要存储到[attr.data-tag]中的值.

    【讨论】:

      【解决方案2】:

      您的代码在我看来是正确的。我尝试将它添加到现有的 stackblitz 示例中,它显示在 HTML 中。也许这将有助于弄清楚:

      https://stackblitz.com/edit/angular-material-select-compare-with?embed=1&file=app/app.html

      <mat-option class="mat-option ng-star-inserted" data-tag="Three" role="option" ng-reflect-value="[object Object]" tabindex="0" id="mat-option-29" aria-selected="false" aria-disabled="false">
      

      【讨论】:

      • 您好@MattNienow,非常感谢您的回答和回复时间。我是 Angular 的新手。我试图访问运行您的代码的链接,但是,抱歉,也许我做错了什么,因为我在网页的源代码中没有看到 data-tag 属性...我实现了一个截屏视频,向您展示我拥有的东西完成:drive.google.com/open?id=1EQycenZ4Evd-TZ8TJljJn7sMJcw3ZFi9
      • @RiccardoVolpe 您的屏幕截图不公开。您是否期望 data-tag 出现在 mat-select 元素上。打开菜单时,它在 mat-option 元素上。
      • 你好@MattNienow,试试这个:drive.google.com/file/d/1EQycenZ4Evd-TZ8TJljJn7sMJcw3ZFi9/… ...我会将选定的选项存储到数据标签中。目的是通过对 data-tag 属性的内容应用后处理来根据输入的表单值生成自动标签......也就是说,循环进入表单以获取所有数据标签属性(基于输入值)它应用后处理来生成标签
      猜你喜欢
      • 2019-07-27
      • 2019-05-10
      • 2018-12-10
      • 1970-01-01
      • 2019-03-20
      • 1970-01-01
      • 2018-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多