【发布时间】:2018-11-06 18:05:12
【问题描述】:
我是 firebase 和 angular 的新手,一步步尝试学习我在 firebase 中创建的数据,现在想进入 angular 5。但它不起作用。
category.service.ts:
import { Injectable } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class CategoryService {
constructor(private db: AngularFireDatabase) { }
getCategories(){
return this.db.list('/categories').valueChanges();
}
}
Product-form.component.ts
import { CategoryService } from './../../category.service';
import { Component, OnInit } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
import { Observable } from 'rxjs/Observable';
@Component({
selector: 'app-product-form',
templateUrl: './product-form.component.html',
styleUrls: ['./product-form.component.css']
})
export class ProductFormComponent implements OnInit {
categories$; // Observable
constructor(private categoryService: CategoryService) {
}
ngOnInit() {
this.categories$ = this.categoryService.getCategories();
console.log(this.categories$);
}
}
product-form.component.html
<select class="form-control" name="" id="category">
<option value=""></option>
<option *ngFor="let c of categories$ | async" [value]="c.$key">
{{c.name}}
</option>
</select>
我得到的控制台错误:
请告诉我我的代码有什么问题。
【问题讨论】:
-
可以添加整个错误日志吗?
-
console.log(this.categories$)说什么? -
Observable {_isScalar: false, source: Observable, operator: MapOperator} operator : MapOperator project : ƒ (actions) thisArg : undefined proto : Object source : Observable _isScalar : false _subscribe : ƒ (subscriber) proto : Object _isScalar : false proto : Object
-
嗨,你有没有解决你的问题..