angular - 获取一个可观察到的 HTML
问题描述
我想在 HTML 中显示一个 observable,我的代码不起作用。
@Component({
selector: 'app-root',
template: '<p *ngFor="let test of map | async">Hier kommt: {{test}}</p>',
styleUrls: ['./app.component.css']})
export class AppComponent{
public sourceOne = of(4, 5, 6 );
public map = this.sourceOne.pipe(map(val => val * 10)).subscribe(val => console.log(val));}
解决方案
您的实施存在很多问题。
您正在订阅 Observable,它将为您提供 aSubscription
而不是Observable
.
再加上你想要做的是map
一个数组的元素。您将得到的响应map
将是一个Array
而不是一个数字。
因此,在map
Rxjs 运算符中,您还必须使用map
数组的运算符。
试试这个:
import { Component } from '@angular/core';
import { of } from 'rxjs';
import { map } from 'rxjs/operators';
@Component({
selector: 'app-root',
template: '<p *ngFor="let test of map | async">Hier kommt: {{test}}</p>',
styleUrls: ['./app.component.css']
})
export class AppComponent {
public sourceOne = of([4, 5, 6]);
public map = this.sourceOne.pipe(map(res => res.map(num => num * 10)));
}
这是您参考的工作示例 StackBlitz。
推荐阅读
- python - pd.DataFrame.from_dict(data) 的 Spark 数据帧版本
- javascript - 如何使用 Choicesjs ajax?
- sqlite - 从对列具有特定值的记录中选择 SQLite 中的随机记录
- snowflake-cloud-data-platform - 雪花数据仓库 - 使用字母数字字符而不是整数生成维度 id
- batch-file - 有没有办法通过写入自身来在批处理文件中创建持久变量?
- active-directory - PassportJS 错误:当绑定间歇性未完成时,不能对连接执行其他操作
- arrays - 如何在'awk'中插入一个数组?
- c# - 如何使用富文本框输入更改面板可见性
- python - 如何返回列表中数字总和的排序列表?
- mysql - 如何检查 mysql 中的值以获取前面的行并使用它们来查找总和