javascript - 如何在 ts 而不是 HTML 中使用管道

标签 javascript angular pipe

我从 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/

相关文章:

javascript - Typescript 导出模块错误

javascript - 是否可以从浏览器外部的共享文件夹中打开文件

angular - 嵌套路由: how to route inside a route?

node.js - 如何手动将管道刷新到 node.js 中的子进程?

C++流输入过程

javascript - 在本地 Linux 机器上部署 spine.js 应用程序

javascript - 按值在深层嵌套对象中查找对象

Angular2 EventEmitter 和 preventDefault()

css - 如何为 p-dialog 中的元素添加样式?

无法从C 中的管道中执行读取?