angular - 如何将任何 [] 转换为可观察的在角度 6
问题描述
我有一个countryList : any[]
具有以下数据的变量:
[
{"countrycode":"SH","countryname":"Saint Helena"},
{"countrycode":"KN","countryname":"Saint Kitts and Nevis"},
{"countrycode":"LC","countryname":"Saint Lucia"},
{"countrycode":"IS","countryname":"Iceland"},
{"countrycode":"IN","countryname":"India"},
{"countrycode":"ID","countryname":"Indonesia"},
{"countrycode":"IR","countryname":"Iran (Islamic Republic of)"},
{"countrycode":"IQ","countryname":"Iraq"},
{"countrycode":"IE","countryname":"Ireland"},
{"countrycode":"IM","countryname":"Isle of Man"},
{"countrycode":"IL","countryname":"Israel"},
{"countrycode":"SH","countryname":"Saint Helena"},
{"countrycode":"KN","countryname":"Saint Kitts and Nevis"},
{"countrycode":"LC","countryname":"Saint Lucia"},
{"countrycode":"ZM","countryname":"Zambia"},
{"countrycode":"ZW","countryname":"Zimbabwe"}
]
我想将其转换为可观察的
这该怎么做 ?
解决方案
如果您使用的是 rxjs 6,您可以简单地使用:
import { of } from 'rxjs';
// ...
let obs = of([
{"countrycode":"SH","countryname":"Saint Helena"},
{"countrycode":"KN","countryname":"Saint Kitts and Nevis"},
// etc...
])
如果您使用的是旧版本,请替换of
为Observable.of
推荐阅读
- html - 父元素有填充时错误的最大高度
- dart - 如果没有可用的网络连接,flutter firestore setData 没有错误
- node.js - 测试 Nodejs 共享实例的最佳实践
- python - 返回时可以留下文件而不关闭吗?
- angular - Angular - 使用 + 符号查询参数 - 以编码的 %2B 形式发送
- android - 无法使用 Android 手机检测到 Digital Persona 5100 设备
- java - 测试 MySQL DB 连接,当打开同一服务器中不同 DB 的 2 个端口时,同时给出连接正确的端口
- opencart - 如何在 Opencart 2.0 中创建产品注册表单
- node.js - 当我尝试发送电子邮件时,ESOCKET 错误是什么意思?
- azure - Databricks:Data Lake Storage Gen 2 的挂载和直接访问之间的区别