我想从Angular firebase数据库中过滤某些内容。为此,我从div中读出了文本。这给了我应该过滤我的数据库的文本。我将此值转移到我的服务中,在这里我想将其用作变量并通过该变量进行过滤,但这是行不通的。
这是我的服务:
cvSkillCollectionFilter: AngularFirestoreCollection<IntCvSkill>;
cvSkillCollectionFilterAnwenden: AngularFirestoreCollection<IntCvSkill>;
cvSkillFilter: Observable<IntCvSkill[]>;
cvSkillFilterAnwenden: Observable<IntCvSkill[]>;
filterWert: string;
constructor(public afs: AngularFirestore) {
this.filterWert = 'Entwicklung';
this.cvSkillCollectionFilter = this.afs.collection('cv-skill', ref => ref.where('skillGruppe', '==', this.filterWert));
this.cvSkillCollectionFilterAnwenden = this.afs.collection('cv-skill');
this.cvSkillFilter = this.cvSkillCollectionFilter.snapshotChanges().pipe(map(changes => {
return changes.map(a => {
const data = a.payload.doc.data() as IntCvSkill;
data.id = a.payload.doc.id;
return data;
});
}));
this.cvSkillFilterAnwenden = this.cvSkillCollectionFilterAnwenden.snapshotChanges().pipe(map(changes => {
return changes.map(a => {
const data = a.payload.doc.data() as IntCvSkill;
data.id = a.payload.doc.id;
return data;
});
}));
}
getSkillsGefiltert() {
return this.cvSkillFilter;
}
getSkillsFilterAnwenden() {
return this.cvSkillFilterAnwenden;
}
addItem(skill: IntCvSkill) {
this.cvSkillCollectionFilter.add(skill);
}
filtere(filterElement: string) {
this.filterWert = filterElement;
console.log(this.filterWert);
}
函数“ filtere()”更改变量“ filterWert”的值。这样做很好,因为当我进行console.log记录时,我得到了正确的值,但是cvCollectionFilter内部的值没有改变,我也不知道为什么。
这是要过滤的属性的元素的componont.ts文件:
skillGruppen: IntCvSkill[];
constructor(private skillService: CvServiceService) { }
ngOnInit() {
this.skillService.getSkillsFilterAnwenden().subscribe(cvSkillGruppe => {
this.skillGruppen = cvSkillGruppe;
});
}
filterAnwenden(filterElement: string) {
this.skillService.filtere(filterElement);
}
这是应该过滤的elemts的componont.ts文件:
SKILLS: IntCvSkill[];
constructor(private skillService: CvServiceService) { }
ngOnInit() {
this.skillService.getSkillsGefiltert().subscribe(cvSkill => {
this.SKILLS = cvSkill;
});
}
这是要过滤的属性的元素的componont.html文件:
<div>Filter Gruppen:</div>
<div *ngFor="let skillGruppe of skillGruppen">
<div (click)="filterAnwenden(filter.textContent)" #filter>{{skillGruppe.skillGruppe}}</div>
</div>
这是应该过滤的elemts的componont.ts文件:
<div *ngIf="SKILLS?.length > 0; else noItem">
<div *ngFor="let skill of SKILLS">
<div class="skill">
<mat-accordion>
<mat-expansion-panel>
<mat-expansion-panel-header>
<mat-panel-title>{{skill.skillname}}
</mat-panel-title>
<mat-progress-bar class="progress-bar" [value]="skill.skillwert"></mat-progress-bar>
<mat-panel-description>
</mat-panel-description>
</mat-expansion-panel-header>
<div>{{skill.skillBeschreibung}}</div>
</mat-expansion-panel>
</mat-accordion>
</div>
</div>
</div>
<ng-template #noItem>
<div>Keine Skills!</div>
</ng-template>
奇怪的是,当我在代码中设置要由mySelf过滤的值时,它就可以工作。如何更改cvSkillCollectionFilter内部的filterWert变量的值?
更新: 问题是,在构造函数中我无法更新变量filterWert。我现在正在寻找一种解决方案。
答案 0 :(得分:1)
在文档Dynamic querying中对此进行了描述。
使用Subject
作为过滤器值,并将此Subject映射到您的查询。
private filterWert$ = new Subject<string>();
constructor(public afs: AngularFirestore) {
this.cvSkillFilter = filterWert$.pipe(
switchMap(filterWert =>
this.afs.collection('cv-skill', ref => ref.where('skillGruppe', '==', filterWert)).snapshotChanges()
),
map(changes => changes.map(a => {
const data = a.payload.doc.data() as IntCvSkill;
data.id = a.payload.doc.id;
return data;
}))
);
}
getSkillsGefiltert() {
return this.cvSkillFilter;
}
filtere(filterElement: string) {
this.filterWert$.next(filterElement);
}
答案 1 :(得分:0)
我用管道解决了问题。
<?php
if($Variable == true)
header("Status: 404 Not Found");
else
header("HTTP/1.1 404 Not Found");
所以我可以直接过滤* ngFor。要更改该值,只需将文本作为变量发送到变量“ skillGruppe”
transform(skills: any, skillgruppe: any): any {
if (skillgruppe === undefined) {
return skills;
}
return skills.filter(skill => {
return skill.skillGruppe.toLowerCase().includes(skillgruppe.toLowerCase());
});
}