选择选项中的 Angular 4 更新列表

标签 angular typescript

<select [(ngModel)]="yearSelected" id="year" (click)="filterYear()">
    <option value="" disabled selected>Year</option>
    <option *ngFor="let y of yearsUnique" [value]="y">{{y}}</option>
</select>
<select [(ngModel)]="facultySelected" id="faculty" (click)="filterFaculty()"> 
    <option value="" disabled selected>Faculty</option>
    <option *ngFor="let f of facultiesUnique" [value]="f">{{f}}</option>
</select>

用户选择年份后,在我的 TypeScript 类中,我更新了包含同一年院系的 facultiesUnique 列表(在初始加载时它加载了所有院系):

filterYear() {
    setTimeout(() => {        
        console.log("this.yearSelected " + this.yearSelected);        
        this.filterList = this.courseList.filter((course: CourseModel) =>
            (course.Year.toString().indexOf(this.yearSelected.toString()) !== -1));
        this.facultiesUnique = this.courseList.filter((course: CourseModel) =>
            (course.Year.toString().indexOf(this.yearSelected.toString()) !== -1));
        this.facultiesUnique = this.courseList.map(data => data.Faculty);      
    }, 500);
}

但是,它不会用相关列表更新下拉列表。 有任何想法吗? 谢谢

最佳答案

您可以使用 (change)="filterYear($event.target.value)" 代替 (click)="filterYear()"

我已经在 Stackblitz 上创建了一个演示

component.html

<select [(ngModel)]="yearSelected" (change)="filterYear($event.target.value)">
    <option value="" disabled selected>Year</option>
    <option *ngFor="let y of yearsUnique" [value]="y">{{y}}</option>
</select>
<select [(ngModel)]="facultySelected"  (change)="filterFaculty($event.target.value)">
    <option value="" disabled selected>Faculty</option>
    <option *ngFor="let f of facultiesUnique" [value]="f.name">{{f.name}}</option>
</select>

component.ts

get facultiesUnique() {
    return this.dummyData.filter(items => {
      return items.year == this.yearSelected
    })
}

关于选择选项中的 Angular 4 更新列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51877990/

相关文章:

javascript - typescript 中的 ES6 map

javascript - 如何处理 Angular 5 递归未知确切数字路由器参数?

reactjs - 有没有办法提取 JSX 元素的 Prop 类型?

javascript - 未捕获的语法错误 : Unexpected token < VM105:17Uncaught ReferenceError: System is not defined

angular - 如何在 Angular 2 中订阅 DOMContentLoaded 事件?

html - 如何在 Angular4 的片段轮播中动态绑定(bind)产品

typescript - 如何使用响应式(Reactive)方法设置 Vue 3 Composition API(Typescript)待办事项列表应用程序

typescript - Vuejs Typescript 类组件 refs.focus 不工作

javascript - 使用 jasmine/karma、spyOn 进行 Angular 组件测试

html - 在 typescript 中渲染图像时使用 onload 函数