如何在Angular(v9,Material)中导出或获取* ngFor循环的LOCAL变量 [英] How to export or get LOCAL variables of a *ngFor loop OUTSIDE it in Angular (v9, Material)

查看:86
本文介绍了如何在Angular(v9,Material)中导出或获取* ngFor循环的LOCAL变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Angular的第一步(版本9的Material).我正在使用以下代码创建表单:

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

上面的代码没有向我返回正确的data-tag-subjectName属性,因为我在HTML代码的mat-option中将[value]设置为subject.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.

正如代码注释中所写,我想将subjectSemestersubjectName(属于 TS object的)subjectSemestersubjectName都导出到*ngFor循环之外,以例如,将它们存储在各自的数据属性(data-tag-subjectSemesterdata-tag-subjectName)中,并在我的模板中将它们也用作label和/或mat-hint,但是由于某些原因我无法执行,例如:

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

我尝试通过将mat-option用于mat-option的代码更改为HTML代码,将value导出为object来尝试选择所需的变量,就像上面修改的最后一个onChange函数一样,但是以此方式,我获得了undefined变量,因此表单中的空字段...

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...

理想情况下,我想使以下代码正常工作:

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.

推荐答案

找到了解决方案,直到找到更好的解决方案为止:

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

这篇关于如何在Angular(v9,Material)中导出或获取* ngFor循环的LOCAL变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆