Angular 按类别筛选列表-角度8
我有一个来自firebase的列表,我想筛选并显示每个类别下的专业化。当我记录控制台日志时,类别和专门化都会被检索到,但我不知道在选择类别时,我需要做什么才能在UI中呈现专门化。任何指导都将不胜感激。谢谢以下是我的工作: category.JSON { “-Lq2PAU_P-fPniAMrQ85”:{ “名称”:“测试” }, “会计财务”:{ “名称”:“会计和财务” }, “保证审计”:{ “名称”:“保证和审计” }, “风险管理”:{ “名称”:“风险管理” }, “税收”:{ “名称”:“税务” } } categories.JSONAngular 按类别筛选列表-角度8,angular,rxjs,Angular,Rxjs,我有一个来自firebase的列表,我想筛选并显示每个类别下的专业化。当我记录控制台日志时,类别和专门化都会被检索到,但我不知道在选择类别时,我需要做什么才能在UI中呈现专门化。任何指导都将不胜感激。谢谢以下是我的工作: category.JSON { “-Lq2PAU_P-fPniAMrQ85”:{ “名称”:“测试” }, “会计财务”:{ “名称”:“会计和财务” }, “保证审计”:{ “名称”:“保证和审计” }, “风险管理”:{ “名称”:“风险管理” }, “税收”:{ “名
{
"accountingFinance" : [ null, "Accounting Management Information Systems", "Accounting Records Maintenance", "Accounts Preparation", "Accountancy / Finance Training" ],
"assuranceAudit" : [ null, "Asset Management Review", "Assurance / Audit Training", "Climate Change / Sustainability Audit", "Enviromental Audit" ],
"riskManagement" : [ null, "Acturial Service", "Enterprise Risk Management", "Fraud Risk Management", "Political Risk Management" ],
"taxation" : [ null, "Business Income Tax", "Capital Gains Tax", "Corporation Tax", "Employee Tax (PAYE)", "Export Incentives" ]
}
HTML标记
<div class="row">
<div class="col-4">
<div class="list-group">
<a
*ngFor="let c of (category$ | async)"
routerLink="/admin/expert-category" [queryParams]="{ category: c.key }"
class="list-group-item list-group-item-action"
[class.active]="category === c.key">
{{ c.name }}
</a>
</div>
</div>
<div class="col">
<div class="row">
<ng-container *ngFor="let categories of filteredCategories; let i = index">
<div class="col">
<div class="card">
<!--<div class="card-body">-->
<ul class="list-group list-group-horizontal">
<li class="list-group-item">{{ categories }}</li>
</ul>
<!--/div>-->
</div>
</div>
<div *ngIf="(i+1) % 4 === 0" class="-w-100"></div>
</ng-container>
</div>
</div>
</div>
目前我没有收到任何错误,除了在选择类别时控制台中有一个空数组。我已经用代码创建了一个空数组
上面提供的html有一些小的修改。还有一个“firebase”模拟,因此您可以看到我也在使用您上面提供的数据。您可能会想在那里查看完整的示例
此外,下面使用的许多方法都来自于像和其他人这样的优秀演讲者
关于你关于过滤的问题
import { Component } from '@angular/core';
import { FirebaseStub } from './firebase.stub';
import { Observable, BehaviorSubject, Subject } from 'rxjs';
import { mergeMap, map, tap } from 'rxjs/operators';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular';
private selectedCategory = new BehaviorSubject<string>('accountingFinance');
category$: Observable<any>;
selectedCategory$ = this.selectedCategory.asObservable();
categories$: Observable<any>;
constructor(private firebaseStub: FirebaseStub) {
this.categories$ = this.selectedCategory$
.pipe(
mergeMap(selectedCategory => this.firebaseStub
.categories$
.pipe(map((category: any) => category[selectedCategory]))
)
);
this.category$ = firebaseStub.category$
.pipe(
tap((category: any) => this.selectedCategory.next('accountingFinance')),
map(categoryObj => Object.keys(categoryObj).map((key,index) => categoryObj[key].name))
);
}
}
从'@angular/core'导入{Component};
从“./firebase.stub”导入{firebasetub};
从“rxjs”导入{可观察,行为主体,主体};
从“rxjs/operators”导入{mergeMap,map,tap};
@组成部分({
选择器:“我的应用程序”,
templateUrl:“./app.component.html”,
样式URL:['./app.component.css']
})
导出类AppComponent{
名称='角度';
private selectedCategory=新行为主体(“会计财务”);
类别$:可以观察到使用不纯净管道方法的一些低效。如果在预览窗格中打开控制台,您可以看到不纯净管道被调用的次数,即使是与完全无关的操作。每次调用它时,ui都会被重新引用。最好的方法是使用自定义管道过滤任何内容。@Mises否根据他们的文档:我在你的JSON示例中没有看到category
,但是你在(s.category
)@peineary开发工作中使用了它。请看?@Mises我添加了一个演示为什么不建议为此使用管道的示例。
export class ExpertCategoryComponent implements OnInit {
category$;
category: string;
closeResult: string;
filteredCategories: any[] = [];
specialization: any[] = [];
constructor(
private categoryService: CategoryService,
route: ActivatedRoute,
private router: Router,
private modalService: NgbModal) {
this.categoryService.getCategories().subscribe(specialization => {
this.specialization = specialization;
console.log(this.specialization);
route.queryParamMap.subscribe(params => {
this.category = params.get('category');
this.filteredCategories = (this.category) ? this.specialization.filter(s => s.category === this.category) : this.specialization;
console.log(this.filteredCategories);
});
});
this.category$ = this.categoryService.getAll();
}
import { Component } from '@angular/core';
import { FirebaseStub } from './firebase.stub';
import { Observable, BehaviorSubject, Subject } from 'rxjs';
import { mergeMap, map, tap } from 'rxjs/operators';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular';
private selectedCategory = new BehaviorSubject<string>('accountingFinance');
category$: Observable<any>;
selectedCategory$ = this.selectedCategory.asObservable();
categories$: Observable<any>;
constructor(private firebaseStub: FirebaseStub) {
this.categories$ = this.selectedCategory$
.pipe(
mergeMap(selectedCategory => this.firebaseStub
.categories$
.pipe(map((category: any) => category[selectedCategory]))
)
);
this.category$ = firebaseStub.category$
.pipe(
tap((category: any) => this.selectedCategory.next('accountingFinance')),
map(categoryObj => Object.keys(categoryObj).map((key,index) => categoryObj[key].name))
);
}
}