如何从 angularfire2 列表中访问 child 节点的值?
How to access values of child node from angularfire2 list?
我有一个 angularfire2 列表,用于显示 post 的列表。每个 post 包含一个 child 节点(收藏夹),其中包含将其标记为收藏夹的用户 ID。
我希望能够将收藏夹与当前用户 ID 进行比较,以确定是否显示添加到收藏夹按钮。
我的问题是,如何访问 child 节点收藏夹中的值以检查它是否包含当前用户,以便我可以使用 *ngIf 来隐藏按钮?
示例模板:
<ion-card *ngFor="let item of posts | async">
<img src="{{ item.img }}" />
<ion-card-content>
<button ion-button (click)="postsProvider.addFavourite(item.$key)">add to favourites</button>
<p>
{{ item.reason }}
</p>
</ion-card-content>
</ion-card>
示例结构:
"reasons" : {
"-KsjNGF9MOj9PYVtcP0F" : {
"favourites" : {
"wYWVy8eR85btVhLw7ra9eLSce632" : true,
"l6ivMgzNE9ZUmHQRwySoaMgiW0J3" : true
},
"img" : "https://firebasestorage.googleapis.com",
"reason" : "asadafaf",
"userId" : "wYWVy8eR85btVhLw7ra9eLSce632"
},
"-KsjWsxJT14IZT-u-mSZ" : {
"dateCreated" : "Tue Aug 29 2017 18:38:02 GMT+0000 (GMT)",
"favourites" : {
"l6ivMgzNE9ZUmHQRwySoaMgiW0J3" : true
},
"img" : "https://firebasestorage.googleapis.com/v0/b/",
"reason" : "asdasdasda",
"userId" : "wYWVy8eR85btVhLw7ra9eLSce632"
},
}
我一直在研究应用程序的其他部分,即显示用户收藏夹的页面,我发现了一些对我来说很奇怪的东西。
收藏页使用以下函数获取收藏的条目
getUserFavourites() {
let user = firebase.auth().currentUser.uid;
return this.db.list('/reasons/', {
query: {
orderByChild: 'favourites/' + user + '/favourited',
equalTo: true
}
});
}
然后我显示这样的信息:
<ion-content>
<ion-card *ngFor="let item of posts | async" (click)="itemTapped($event, item)">
<div class="card-img" [style.backgroundImage]="'url(' + item.img + ')'"></div>
<div class="remove-favourite" (click)="postsProvider.removeFavourite(item.$key)">
<ion-icon name="close"></ion-icon>
</div>
<ion-card-content>
<ion-card-title>
{{ item.reason }}
</ion-card-title>
<p>
{{ item.favourites[userId].note }}
</p>
</ion-card-content>
</ion-card>
</ion-content>
使用此方法我可以使用 {{ item.favourites[userId].note }} 并返回值。但是,除了稍微不同的查询(如下)之外,几乎相同的设置都不起作用。
getPosts(limit) {
return this.db.list('/reasons', {
query: {
limitToFirst: limit
}
});
}
我觉得我遗漏了一些明显的东西,但我不明白为什么结果之间会有差异,除了它们使用的查询略有不同。
我已经找到导致问题的原因。
当我使用 *ngIf="post.favourites[userId].favourited !== true" 或尝试显示值时,如果 post 没有任何收藏夹,则会显示错误。
为了解决这个问题,我添加了一个检查以确保通过添加 or 语句
来设置 post.favourites
*ngIf="!post.favourites || post.favourites[userId].favourited !== true"
我有一个 angularfire2 列表,用于显示 post 的列表。每个 post 包含一个 child 节点(收藏夹),其中包含将其标记为收藏夹的用户 ID。
我希望能够将收藏夹与当前用户 ID 进行比较,以确定是否显示添加到收藏夹按钮。
我的问题是,如何访问 child 节点收藏夹中的值以检查它是否包含当前用户,以便我可以使用 *ngIf 来隐藏按钮?
示例模板:
<ion-card *ngFor="let item of posts | async">
<img src="{{ item.img }}" />
<ion-card-content>
<button ion-button (click)="postsProvider.addFavourite(item.$key)">add to favourites</button>
<p>
{{ item.reason }}
</p>
</ion-card-content>
</ion-card>
示例结构:
"reasons" : {
"-KsjNGF9MOj9PYVtcP0F" : {
"favourites" : {
"wYWVy8eR85btVhLw7ra9eLSce632" : true,
"l6ivMgzNE9ZUmHQRwySoaMgiW0J3" : true
},
"img" : "https://firebasestorage.googleapis.com",
"reason" : "asadafaf",
"userId" : "wYWVy8eR85btVhLw7ra9eLSce632"
},
"-KsjWsxJT14IZT-u-mSZ" : {
"dateCreated" : "Tue Aug 29 2017 18:38:02 GMT+0000 (GMT)",
"favourites" : {
"l6ivMgzNE9ZUmHQRwySoaMgiW0J3" : true
},
"img" : "https://firebasestorage.googleapis.com/v0/b/",
"reason" : "asdasdasda",
"userId" : "wYWVy8eR85btVhLw7ra9eLSce632"
},
}
我一直在研究应用程序的其他部分,即显示用户收藏夹的页面,我发现了一些对我来说很奇怪的东西。
收藏页使用以下函数获取收藏的条目
getUserFavourites() {
let user = firebase.auth().currentUser.uid;
return this.db.list('/reasons/', {
query: {
orderByChild: 'favourites/' + user + '/favourited',
equalTo: true
}
});
}
然后我显示这样的信息:
<ion-content>
<ion-card *ngFor="let item of posts | async" (click)="itemTapped($event, item)">
<div class="card-img" [style.backgroundImage]="'url(' + item.img + ')'"></div>
<div class="remove-favourite" (click)="postsProvider.removeFavourite(item.$key)">
<ion-icon name="close"></ion-icon>
</div>
<ion-card-content>
<ion-card-title>
{{ item.reason }}
</ion-card-title>
<p>
{{ item.favourites[userId].note }}
</p>
</ion-card-content>
</ion-card>
</ion-content>
使用此方法我可以使用 {{ item.favourites[userId].note }} 并返回值。但是,除了稍微不同的查询(如下)之外,几乎相同的设置都不起作用。
getPosts(limit) {
return this.db.list('/reasons', {
query: {
limitToFirst: limit
}
});
}
我觉得我遗漏了一些明显的东西,但我不明白为什么结果之间会有差异,除了它们使用的查询略有不同。
我已经找到导致问题的原因。
当我使用 *ngIf="post.favourites[userId].favourited !== true" 或尝试显示值时,如果 post 没有任何收藏夹,则会显示错误。
为了解决这个问题,我添加了一个检查以确保通过添加 or 语句
来设置 post.favourites*ngIf="!post.favourites || post.favourites[userId].favourited !== true"