将数组发送到管道

问题描述 投票:0回答:1

我正在尝试通过*ngFor将数组发送到管道以随机化所有数组,如下所示:

<div class="questions" *ngFor="let question of this.allQuestions | randomPipe ; let index = index ">

Random-pipe.pipe.ts中,我有以下代码:

import { Pipe, PipeTransform, ɵConsole } from "@angular/core";
import { Question } from "./Modals/question";

@Pipe({
  name: "randomPipe"
})
export class RandomPipePipe implements PipeTransform {
  transform(questions: Question[]): any {
    console.log(questions);
    console.log(questions.length);
  }
}

要点如下:

console.log(questions);返回所有问题(我的模型)。

console.log(questions.length);返回0,即使questions有9个问题。这怎么可能?

如果我尝试执行questions.foreach();来推送到辅助数组,则它不起作用。

let aux: Question[] = new Array();
questions.forEach(element => {
  aux.push(element);
});

辅助为空。我该如何解决?

arrays angular pipe
1个回答
0
投票

我认为您必须创建“管道”链,但首先要开始使用异步管道而不是自定义管道;

<span *ngFor="let content of contentLoop | async | uppercases">
  <p>{{content}}</p>
</span>

我的自定义管道:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'uppercases'
})
export class UppercasesPipe implements PipeTransform {

  transform(value: any, args?: any): any {
    return value ? value.map(el => el.toUpperCase()): [];
  }

}

我制作了此样本:pipeAsync

© www.soinside.com 2019 - 2024. All rights reserved.