我从 ts 添加文本到 html 元素
像这样
this.legend.append('text')
.attr('x', legendRectSize + legendSpacing)
.attr('y', legendRectSize - legendSpacing)
.text(function(d) { return d; });
这将创建类似的 html
<text>Data will come here</text>
我想用管道将这些数据转换成某种形式 我怎样才能从 ts 代码做到这一点?
并且当我动态创建这个 HTML 时,我不能像这样使用管道
<text>{{Data will come here | pipename}} </text>
我在找类似的东西
this.legend.append('text')
.attr('x', legendRectSize + legendSpacing)
.attr('y', legendRectSize - legendSpacing)
.text(function(d) { return d; }).applypipe('pipename');
最佳答案
首先在组件中导入你的管道。然后在你的组件中使用你的管道。像这样..
pipe.ts
/**
* filter checkbox list
*/
@Pipe({ name: 'filter', pure: true })
export class FilterPipe{
transform(items: any[], args: any): any {
let filter = args.toString();
if(filter !== undefined && filter.length !== null){
if(filter.length === 0 || items.length ===0){
return items;
}else{
return filter ? items.filter(item=> item.title.toLocaleLowerCase().indexOf(filter) != -1) : items;
}
}
}
}
component.ts(在您的 typescript 代码中使用)
const filterPipe = new FilterPipe();
const fiteredArr = filterPipe.transform(chkArray,txtSearch);
xyz.html(在您的 html 文件中使用)
<ul>
<li *ngFor="todo for todos | filter:'txtsearch'"> {{todo.name}} </li>
</ul>
关于javascript - 如何在 ts 而不是 HTML 中使用管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39653630/