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

流过昼夜 提交于 2020-04-17 22:53:52

问题


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.


回答1:


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()
            );
    }
}


来源:https://stackoverflow.com/questions/60363462/how-to-export-or-get-local-variables-of-a-ngfor-loop-outside-it-in-angular-v9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!