我对 Angular 比较陌生。我试图使用 @Input() 将数据从父组件传递到子组件,但未反映更改。我有一个带有选择元素的父组件,我想将所选项目的值传递给孩子。父组件有一个带有几个 mat-tab(s) 的 mat-tab-group。
父 HTML
<mat-form-field>
<mat-select placeholder="Class" [formControl]="selectedClassControl" (selectionChange)="changeClassId()">
<mat-option *ngFor="let class of classes" value="{{class.id}}">{{class.classNumber}}-{{class.section}}</mat-option>
</mat-select>
</mat-form-field>
<mat-tab-group color="primary" (selectedIndexChange)="selectTab($event)">
<mat-tab label="Units">
<app-units [classId]="classId" *ngIf="selectedTab === 0"></app-units>
</mat-tab>
<!-- many such tabs with the same structure -->
</mat-tab-group>
家长 TS
classId : number = 0;
classes: Class[];
selectedClass: any;
selectedClassControl = new FormControl(this.selectedClass);
constructor(private classService: ClassService, private stateMgmtService:
StateManagementService) {
var schoolId = this.stateMgmtService.getSchoolId();
this.classService.getClasses(schoolId).subscribe(data =>{
this.classes = data;
})
}
selectedTab = 0;
ngOnInit(): void {
}
selectTab(event) {
this.selectedTab = event;
}
changeClassId(){
this.classId = this.selectedClassControl.value;
console.log("ClassId changed to "+this.classId);
}
子 HTML
<mat-form-field>
<mat-select placeholder="Subject" [formControl]="selectedSubjectControl" >
<mat-option *ngFor="let subject of subjects" value="{{subject.id}}">{{subject.code}}-{{subject.name}}</mat-option>
</mat-select>
</mat-form-field>
儿童 TS
@Input() classId : number = 0;
subjects: Subject[];
selectedSubject: any;
selectedSubjectControl = new FormControl(this.selectedSubject);
constructor(private classService: ClassService) {
console.log("classId"+this.classId);
if(this.classId != 0)
classService.getSubjects(this.classId).subscribe( data => {
this.subjects = data;
})
}
我看到的是,当我在父组件中更改我的选择时,classId 没有传达给子组件,我应该手动刷新组件以使更改得到反映吗?
非常感谢任何帮助...
TIA