【发布时间】:2019-05-27 21:41:54
【问题描述】:
我正在尝试像这样访问 component.html 中的 Observable:
{{ (pins | async)[0]?.state }}
我不知道是否可以这样做,但我想访问 html 中的 Observable 列表的单个部分而不用 *ngFor 循环。 (我正在使用火力基地。) 这是component.ts:
pins: Observable<any[]>;
this.pins = this.db.list('pins').snapshotChanges().pipe(
map(changes =>
changes.map(c => ({ $key: c.payload.key, ...c.payload.val() }))
)
);
这是引脚的json数据:
[
{
"$key": "0",
"pin": 5,
"state": true
},
{
"$key": "1",
"pin": 4,
"state": false
},
{
"$key": "2",
"pin": 0,
"state": true
},
{
"$key": "3",
"pin": 2,
"state": false
},
{
"$key": "4",
"pin": 14,
"state": true
},
{
"$key": "5",
"pin": 12,
"state": true
},
{
"$key": "6",
"pin": 13,
"state": true
},
{
"$key": "7",
"pin": 15,
"state": true
}
]
它适用于第一个项目,但其他项目不工作,并显示此错误:
ERROR TypeError: Cannot read property '0' of null
at Object.eval [as updateRenderer] (HomeComponent.html:88)
at Object.debugUpdateRenderer [as updateRenderer] (core.js:22503)
at checkAndUpdateView (core.js:21878)
at callViewAction (core.js:22114)
at execComponentViewsAction (core.js:22056)
at checkAndUpdateView (core.js:21879)
at callViewAction (core.js:22114)
at execEmbeddedViewsAction (core.js:22077)
at checkAndUpdateView (core.js:21874)
at callViewAction (core.js:22114)
感谢您的帮助!
【问题讨论】:
-
你加
*ngIf="pins"了吗?使用前ping. -
我现在试过了,但还是一样的错误
-
你能创建最小的
https://stackblitz.com/ -
stackblitz.com/edit/angular-1uon89(未连接到我的 Firebase)
-
检查更新的答案
标签: javascript json angular typescript firebase