javascript - 在初始化时将所有过滤器选项设置为 'active' - Angular

标签 javascript angular typescript filter pipe

https://stackblitz.com/edit/timeline-angular-7-wbff3f

上面的 stackblitz 将演示一些管道过滤器。如果您单击“北、南或东”的位置,它将填充 *ngFor 循环 - 这些过滤器可以选择开/关并使用“多过滤器管道”。默认情况下,我希望所有这些过滤器按钮“激活”或“打开”以显示完整的填充列表开始,然后允许您单击过滤器关闭。否则,列表在第一次加载时为空!

.html 文件

<button [class.active]="entry.isLocationActive" (click)="toggle(entry.location); entry.isLocationActive = !entry.isLocationActive" class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique">{{entry.location}}</button>


<my-timeline-entry *ngFor="let entry of timeLine | filter:filteredYear:'year'| multifilter:filteredLocations:'location' " timeEntryHeader={{entry.year}} timeEntryContent={{entry.detail}} timeEntryPlace={{entry.place}} timeEntryLocation={{entry.location}}></my-timeline-entry>

.ts 文件

filteredLocations: string[] = [];

toggle(location) {
    let indexLocation = this.filteredLocations.indexOf(location);
    if (indexLocation >= 0) {
      this.filteredLocations = this.filteredLocations.filter((i) => i !== location);
    } else {
      this.filteredLocations.push(location);
    }
}

我试过用所有值预填充 filteredLocations 对象,所以它从所有值开始,因此都是事件的。

我设法让“事件”类在第一次加载时处于事件状态,但这并没有传递到管道过滤器。我敢肯定它不是一个大的变化只是看不到它,任何帮助将不胜感激!

最佳答案

filteredLocations = this.timeLine.map(a => a.location);

将其添加到 .ts 文件并替换空的 filteredLocations: string[] = [];

最初我离最终结果并不遥远。希望对其他人有帮助。

关于javascript - 在初始化时将所有过滤器选项设置为 'active' - Angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58138265/

相关文章:

javascript - 带有 javascript 循环的 HTML5 视频

javascript - pdf.js 中的错误 : Unknown action from worker: undefined at Worker. MessageHandler._onComObjOnMessage

javascript - Highcharts 不显示具有多个 Y 轴的图形的系列数据

angular - Angular 为 4 的 block (缓存)加载错误

通过使用模型设置值不会触发 Angular 输入事件

Typescript 目标为 es6 和 es2015

angular - 从路由器 URL 获取 ID

javascript - 用JS拖拽后显示图片

angular - 如何让 RxJS observable 管道访问原始 observable 的发射和管道之前的发射?

Angular 2 + Auth0(找不到模块 'auth0-js' 的声明文件。)