我是角度编程的新手,问题肯定很简单,但它让我发疯!
我在firestore上有一个数据库,我正在通过服务从中检索数据。
数据由接口构成(嵌套其他接口,包括数组......)
我的服务:
@Injectable({
providedIn: 'root'
})
export class InfogeneriqueServicesService {
ItemsInfoGeneriqueCollection : AngularFirestoreDocument<Navigation>;
Infogenes: Observable<Navigation>;
constructor(public afs: AngularFirestore) {
this.ItemsInfoGeneriqueCollection = this.afs.doc('user/7878AZEVZAEV4');
this.Infogenes = this.ItemsInfoGeneriqueCollection.valueChanges();
} getInfoSGeneriques(){
return this.Infogenes;
}
我的组件ts:
import {Navigation} from '../StructureInterfaces/structuresInter';
import { InfogeneriqueServicesService } from '../services/infogenerique-services.service';
...
Nav : Navigation;
...
constructor(private adapter: DateAdapter<any>, private NaviService : InfogeneriqueServicesService) {}
ngOnInit(){
this.NaviService.getInfoSGeneriques().subscribe(IDperso =>{
this.Nav = IDperso;
});
this.selectedgrade = this.Grades[this.Nav.id.grade-1].viewValue;
我收到一个错误:
ERROR TypeError:“this.Nav未定义”
是因为此时仍未生成this.Nav吗? this.selectegrade ...指令是否应该等待生成数据(以及如何?)或是否存在代码问题?
我将不胜感激任何帮助......谢谢!
移动用于在getInfoSGeneriques回调函数中设置selectedGrade的代码,因为您正在执行的函数是异步的
ngOnInit(){
this.NaviService.getInfoSGeneriques().subscribe(IDperso =>{
this.Nav = IDperso;
this.selectedgrade = this.Grades[this.Nav.id.grade-1].viewValue;
});
}