selectmultidimensional-arrayangular-materialcustom-data-attributeangular-ngfor

How to write a custom data attribute for a 'mat-select' element in Angular Material 9


I'm trying to set a custom data attribute (data-tag) for a mat-select element in Angular Material 9. The following is the HTML and TS code that I'm using, but nothing happens by inspecting the webpage code into the browser:

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"
                }
            ]
        }
    ];
}

I found this question, but I can't understand what I'm doing wrong. Thank you

#Edit 1:

I tried also:

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

but nothing...


Solution

  • Finally SOLVED:

    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>
    

    As written in the comments of the code, I have to put the [attr.data-tag] into the mat-select equal to this.subjectControl.value, and set [value] of mat-option equal to the value to store into [attr.data-tag].