How to export or get LOCAL variables of a *ngFor loop OUTSIDE it in Angular (v9, Material)
Asked Answered
D

1

0

First steps in Angular (with Material at version 9). I'm creating a form with the following code:

HTML:

<mat-form-field>
    <mat-label>Course</mat-label>
        <mat-select
            [formControl]="subjectControl"
            [attr.data-tag-subjectSemester]="this.subjectControl.value
                ? this.subjectControl.value.trim()
                : ''
            [attr.data-tag-subjectName]="this.subjectControl.value
                ? this.subjectControl.value.trim()
                : ''
            (selectionChange)="onChange($event)"
            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.subjectSemester"><!--For now, here I have to set `[value]` to `.subjectName` OR `.subjectSemester` to show it into the `data-tag`, but the question is How to export BOTH variables outside the `*ngFor` loop, if I can choose only one variable as option to show it as value?-->
                    {{ subject.subjectName }}
                </mat-option>
            </mat-optgroup>
        </mat-select>
        <mat-hint>
            {{
                this.subjectControl.value
                    ? this.subjectControl.value.trim()
                    : ''
            }}
        </mat-hint>
</mat-form-field>

TS:

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

subjectControl = new FormControl("", Validators.required);
export class FormComponent implements OnInit {
    subjectControl = new FormControl("", Validators.required);
    subjects: SubjectGroup[] = [
        {
            disabled: false,
            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"
                }
            ]
        },
        {
            disabled: false,
            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"
                }
            ]
        }
    ];
}

onChange(event: { stopPropagation: () => void }) {
    let subjectSemester = this.subjectControl.value; /* here I would to export both subjectName and subjectSemester; I tried in a way like `let subjectSemester = this.subjectControl.value?.subjectSemester;` and `let subjectName = this.subjectControl.value?.subjectName;`, but I can't get it! What I'm doing wrong?*/
    alert(subjectSemester);
}

The code above doesn't return me the correct data-tag-subjectName attribute, because I set [value] to subject.subjectSemester into the mat-option of my HTML code.

As written in the code's comments, I would like to export outside the *ngFor loop both subjectSemester and subjectName (of the TS object) for the selected option, to store them in the respective data attributes (data-tag-subjectSemester and data-tag-subjectName), for example, and use also them in my template as label and/or mat-hint, but for some reason I can't do, for example:

TS:

onChange(event: { stopPropagation: () => void }) {
    let subjectSemester = this.subjectControl.value?.subjectSemester;
    let subjectName = this.subjectControl.value?.subjectName;
    alert(subjectName + ", " + subjectSemester);
}

I tried to change the code by using [ngValue]="subject" for mat-option into the HTML code, to export the value as object to try to select the wanted variable like in the modified last onChange function above, but in this way I obtain undefined variables and so empty fields in my form...

Ideally, I would like to make the following code working:

HTML:

<mat-form-field>
    <mat-label>Course</mat-label>
        <mat-select
            [formControl]="subjectControl"
            [attr.data-tag-subjectSemester]="this.subjectControl.value?.subjectSemester
                ? this.subjectControl.value?.subjectSemester.trim()
                : ''
            [attr.data-tag-subjectName]="this.subjectControl.value.?subjectName
                ? this.subjectControl.value?.subjectName.trim()
                : ''
            (selectionChange)="onChange($event)"
            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.subjectSemester">
                    {{ subject.subjectName }}
                </mat-option>
            </mat-optgroup>
        </mat-select>
        <mat-hint>
            {{
                this.subjectControl.value?.subjectSemester
                    ? this.subjectControl.value?.subjectSemester.trim()
                    : ''
            }}
        </mat-hint>
</mat-form-field>

TS:

onChange(event: { stopPropagation: () => void }) {
    let subjectSemester = this.subjectControl.value?.subjectSemester;
    let subjectName = this.subjectControl.value?.subjectName;
    alert(subjectName + ", " + subjectSemester);
}

Hope to understand what I'm doing wrong, thank you so much.

Diminish answered 23/2, 2020 at 14:56 Comment(0)
D
0

Found a solution, until a better one:

HTML:

<mat-form-field>
    <mat-label>Course</mat-label>
    <mat-select
        id="subjectSelector"
        [formControl]="subjectControl"
        (selectionChange)="onChange($event)"
        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.subjectSemester">
                {{ subject.subjectName }}
            </mat-option>
        </mat-optgroup>
    </mat-select>
</mat-form-field>

TS:

onChange(event: any) {
    if (event.source.selected === undefined) {
        document
            .getElementById("subjectSelector")
            .setAttribute("data-tag-subjectName", "subjectNone");
        document
            .getElementById("subjectSelector")
            .setAttribute("data-tag-subjectSemester", "semesterNone");
    } else {
        let target = event.source.selected._element.nativeElement;
        let selectedData = {
            fieldId: target.getAttribute("id"),
            Semester: event.value,
            Name: target.innerText.trim()
        };
        //console.log(selectedData);
        //alert(selectedData.Semester);
        //alert(selectedData.Name);
        document
            .getElementById("subjectSelector")
            .setAttribute(
                "data-tag-subjectName", selectedData.Name.trim()
            );
        document
            .getElementById("subjectSelector")
            .setAttribute(
                "data-tag-subjectSemester", selectedData.Semester.trim()
            );
    }
}
Diminish answered 25/2, 2020 at 1:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.