Angular 的第一步(使用版本 9 的材料)。我正在使用以下代码创建一个表单:
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 代码中设置[value]
了。subject.subjectSemester
mat-option
正如代码注释中所写,我想将所选选项的和(的TS*ngFor
)都导出到循环之外,以将它们存储在相应的数据属性(和)中,例如,并在我的模板中使用它们作为和/或,但由于某种原因我不能这样做,例如:subjectSemester
subjectName
object
data-tag-subjectSemester
data-tag-subjectName
label
mat-hint
TS:
onChange(event: { stopPropagation: () => void }) {
let subjectSemester = this.subjectControl.value?.subjectSemester;
let subjectName = this.subjectControl.value?.subjectName;
alert(subjectName + ", " + subjectSemester);
}
我尝试通过使用[ngValue]="subject"
for更改代码mat-option
到 HTML 代码中,导出value
asobject
以尝试选择想要的变量,就像onChange
上面修改后的最后一个函数一样,但是通过这种方式,我undefined
在表单中获得了变量和空字段......
理想情况下,我想让以下代码工作:
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);
}
希望能理解我做错了什么,非常感谢。