我可以从管道调用 Angular2 管道吗? [英] Can I call an Angular2 pipe from a pipe?
问题描述
我从服务器返回一个对象,该对象包含一个列数组及其定义和一个行数组.我想在构建 HTML 表格时迭代列和行,并根据使用格式"管道返回的列类型设置单元格格式.
I get back from the server an object containing an array of columns with their definitions and an array of rows. I would like to iterate over the columns and rows as I build my HTML table and format the cells according to the column type returned using a "format" pipe.
例如
WS 响应:
{
"columns" [{"precision":10,"name":"PAGES","typeName":"INTEGER","scale":0...,
"rows":[{"PAGES":6531....}, [{"PAGES":6531....}]
}
HTML 片段:
<tr *ngFor="let row of invoices?.rows">
<td *ngFor="let column of invoices?.columns>
{{row[column.name] | format : column}}
</td>
</tr>
根据列的类型,我的格式"管道有什么办法可以充当正确的内置管道(其中一个退出)的委托人?我不想重新实现 DecimalPipe、DatePipe 等.
Is there any way my "format" pipe can just act a a delegator to the correct built-in pipe (where one exits), depending on the type of the column? I don't want to have to re-implement DecimalPipe, DatePipe, etc. etc.
推荐答案
是的,您可以调用管道 - 只需设置它们并调用 transform
:
Yes you can call pipes - just instatiate them and call transform
:
transform(cell: any, column: any): any {
if (column.typeName === "INTEGER") {
let pipe = new DecimalPipe();
return pipe.transform(cell, "1.0-0");
}
// more here ...
}
这篇关于我可以从管道调用 Angular2 管道吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!