我正在尝试通过*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);
});
辅助为空。我该如何解决?
我认为您必须创建“管道”链,但首先要开始使用异步管道而不是自定义管道;
<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