javascript - 访问 Firebase 对象并将其映射到基于 Angular 的应用程序中的 UI
问题描述
我正在尝试从我的 Nativescript Angular 应用程序访问 firebase 实时数据库中的这个数据样本(评论),以在下面的 xml 代码中呈现评论。
此代码适用于导入到我的数据库的 JSON 数据,但不适用于启动 push() 请求后由 firebase 创建的自动生成的字母数字 ID。(如果您用整数替换字符串 id,则代码可以正常工作)。
打字稿文件
export class DishdetailComponent implements OnInit {
dish: Dish;
id: number;
comment: Comment;
errMess: string;
avgstars: string;
numcomments: number;
favorite: boolean;
showComments: boolean = false;
cardImage: View;
commentList: View;
cardLayout: View;
constructor(private dishservice: DishService,
private route: ActivatedRoute,
private routerExtensions: RouterExtensions,
private favoriteService: FavoriteService,
private vcRef: ViewContainerRef,
private modalService: ModalDialogService,
private http: HttpClient,
private page: Page,
@Inject('baseURL') private baseURL) { }
ngOnInit() {
this.route.params
.pipe(switchMap((params: Params) => this.dishservice.getDish(params['id'])))
.subscribe(dish => {
this.dish = dish
this.favorite = this.favoriteService.isFavorite(this.dish.id);
//compute the average of the comments and the render the length of the array
const commentIds = Object.keys(this.dish.comments);
this.numcomments = commentIds.length;
let total = 0;
commentIds.forEach(commentId => total += this.dish.comments[commentId].rating);
this.avgstars = (total/this.numcomments).toFixed(2) || null;
},
errmess => this.errMess = <any>errmess);
}
onDrawerButtonTap(): void {
const sideDrawer = <RadSideDrawer>app.getRootView();
sideDrawer.showDrawer();
}
**Html template file**
<Label row="1" height="40" class="p-10 m-t-10 h3" text="Comments"></Label>
<ListView id="commentList"
row="2"
height="300"
[items]="dish.comments"
class="list-group p-10"
[visibility]="(showComments) ? 'visible': 'collapsed'">
<ng-template let-comment="item">
<StackLayout class="list-group-item">
<Label class="list-group-item-heading" [text]="comment.comment" textWrap="true"></Label>
<StackLayout orientation="horizontal">
<Label class="list-group-item-text" [text]="comment.rating"></Label>
<Label class="list-group-item-text" text=" Stars"></Label>
</StackLayout>
<StackLayout orientation="horizontal">
<Label class="list-group-item-text" [text]="'-- ' + comment.author + ', '"></Label>
<Label class="list-group-item-text" [text]="comment.date | date"></Label>
</StackLayout>
</StackLayout>
</ng-template>
</ListView>
我在我的代码中做错了什么?非常感谢您的审查和更正。
解决方案
推荐阅读
- java - 可变长度参数列表将泛型和泛型数组作为参数传递 - Java
- python - 在 Python 中重命名“_”变量
- php - PHP - Magento 2 通过 Sku 代码加载产品
- c++ - 如何访问 Qt::DisplayRole 并在 TableView 中指定列
- android - 自定义模拟时间选择器
- python - 如何比较python3中的两个频率分布(使用nltk FreqDist创建)?
- c# - 棱镜:在外壳以外的另一个窗口中注册区域
- node.js - 以编程方式构建猫鼬查询
- asp.net-mvc - MVC ActionLink 404 错误
- java - 圆形点计数器 Java 或圆形进度条