小编Luk*_*uke的帖子

循环数组并返回Observable中每个id的数据

使用RxJS v6很难从子集合中检索循环中每个项目的数据。无法迭代从HTTP调用检索的throw数组。合并映射仅对需要在所有数组项目上执行的单个项目执行此操作。

我已经坚持了三天。我已经尝试了一切。问题在于,新的管道语法在为您提供组织代码的巧妙方法的同时,没有简单的方法来循环引发数据收集。无法使用地图javascript函数,因为它在可观察范围之外并且返回的项目不确定。在互联网上,我只能找到一个很好的单个mergeMap的基本示例。但是,我需要使用带有http调用的数组中的每个项目,并将响应附加到同一对象。

我从第三方无法控制的两个端点开始。我嘲笑了一些示例API来说明。

端点1-返回所有项目ID: http:// localhost:3000 / contact

{
  "name": "test",
  "contact": [
    {
      "_id": "5c9dda9aca9c171d6ba4b87e"
    },
    {
      "_id": "5c9ddb82ca9c171d6ba4b87f"
    },
    {
      "_id": "5c9ddb8aca9c171d6ba4b880"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

端点2-返回单个项目信息: http:// localhost:3000 / contact / 5c9dda9aca9c171d6ba4b87e

[
   {
     "_id": "5c9dda9aca9c171d6ba4b87e",
     "firstName": "Luke",
     "lastName": "Test",
     "email": "vgadgf@adsgasdg.com",
     "created_date": "2019-03-29T08:43:06.344Z"
   }
]
Run Code Online (Sandbox Code Playgroud)

期望的结果-通过使用RxJs v6 +返回带有嵌套数据的Observable:

{
  "name": "test",
  "contact": [
    {
      "_id": "5c9dda9aca9c171d6ba4b87e".
      "relationship":  {
                         "_id": "5c9dda9aca9c171d6ba4b87e",
                         "firstName": "Luke",
                         "lastName": "Test",
                         "email": "vgadgf@adsgasdg.com",
                         "created_date": "2019-03-29T08:43:06.344Z"
                       }
    },
    {
      "_id": "5c9ddb82ca9c171d6ba4b87f",
      "relationship": {
                         "_id": …
Run Code Online (Sandbox Code Playgroud)

observable rxjs angular rxjs6 angular7

2
推荐指数
2
解决办法
1622
查看次数

如何在 Angular 中覆盖/匹配扩展的CurrencyPipe方法类型

我正在尝试重用 Angular Common 中的现有货币管道。目标是当值取整时截断 .00。为此我写了这段代码:

/** Transform currency string and round it.  */
@Pipe({name: 'customCurrency'})
export class CustomCurrencyPipe extends CurrencyPipe implements PipeTransform {
  transform(value: number|string|null|undefined): string|null {
    if (!isValue(value)) return null;
    const valueFormat = (+value % 1 === 0) ? '1.0-0' : '1.2-2';
    return super.transform(value, 'USD', 'symbol', valueFormat);
  }
}

function isValue(value: number|string|null|undefined): value is number|string {
  return !(value == null || value === '' || value !== value);
}
Run Code Online (Sandbox Code Playgroud)

如果我将转换类型设置为:any 它运行没有问题。但是我不允许在当前环境中使用任何内容。如果我将其设置为 :string|null 我会收到此错误:

TS2416: Property 'transform' in type 'CustomCurrencyPipe' …
Run Code Online (Sandbox Code Playgroud)

oop typescript angular2-pipe angular

2
推荐指数
1
解决办法
1385
查看次数

标签 统计

angular ×2

angular2-pipe ×1

angular7 ×1

observable ×1

oop ×1

rxjs ×1

rxjs6 ×1

typescript ×1