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

Javascript 如何在ts而不是HTML中使用管道,javascript,angular,pipe,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> 如果Pipename是您的自定义

我将文本从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>

如果Pipename是您的自定义管道,那么如果您希望在ts文件中使用相同的名称,则可以使用以下代码

import {Pipename} from './pipename';

Pipename.prototype.transform(arguments);//this is how u can use your custom pipe

首先在组件中导入管道。然后在组件中使用管道。像这样

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;
        }
    }
  }
}
组件.ts(在您的打字脚本代码中使用)

xyz.html(在html文件中使用)

    {{{todo.name}
在您的.ts中

import {YourPipe} from '/pipePath';


let value = new YourPipe().transform(param);

在组件中导入管道

import { PipeName } from './pipename'
将其包含在提供的文件中

@Component({
    selector: 'pipe-using-component',
    templateUrl: './pipe-using-component.html',
    providers: [
        PipeName
    ],
})
将其注入构造函数中

export class PipeUsingComponent {
  constructor(private pipeName: PipeName)
   }

   var requiredResult = this.pipeName.transform(passvalue);
}

我必须在多个方法上使用它,所以我声明了一个类型为:
PipeName
的属性

private pipeName: PipeName;
然后在构造函数中

constructor() {
    this.pipeName = new PipeName();
}
在任何服务注入的情况下

constructor(
    private anotherService: AnotherService
) {
    this.pipeName = new PipeName(anotherService);
}
使用管道变换.ts中的任意位置

this.pipeName.transform(value, format);
范例

import { Component, OnInit } from '@angular/core';
import { LocalizedDatePipe } from '../../../pipes/localized-date.pipe';
import { TranslateService } from '@ngx-translate/core';

@Component({
    selector: 'edit-appointment',
    templateUrl: './edit-appointment.component.html',
    styleUrls: ['./edit-appointment.component.css']
})
export class EditAppointmentComponent implements OnInit {
    startDate: string;
    localizeDatePipe: LocalizedDatePipe;

    constructor(
        private translateService: TranslateService
    ) {
        this.localizeDatePipe = new LocalizedDatePipe(this.translateService);
    }

    ngOnInit() {
        this.startDate = this.localizeDatePipe.transform('2021-04-08T07:30:00Z', 'd/M/yyyy, h:mm a');
    }
}

请澄清你的问题。我很难理解你在找什么。现在可以了吗@Morkadosh请添加完整的typescript代码。我的问题是diffrenet如何在javascript或typescript代码中使用此管道,而不是在HTML中。我的答案也是在typescript中使用管道。不,在HTML
pure:true
中使用是默认设置。。。()管道不应该被注入构造函数中吗?这正是我想要的,如果管道类中使用了
@pipe
构造函数,你会绕过它,谁知道
@pipe
会添加什么呢?是的!这真的很容易!
constructor() {
    this.pipeName = new PipeName();
}
constructor(
    private anotherService: AnotherService
) {
    this.pipeName = new PipeName(anotherService);
}
this.pipeName.transform(value, format);
import { Component, OnInit } from '@angular/core';
import { LocalizedDatePipe } from '../../../pipes/localized-date.pipe';
import { TranslateService } from '@ngx-translate/core';

@Component({
    selector: 'edit-appointment',
    templateUrl: './edit-appointment.component.html',
    styleUrls: ['./edit-appointment.component.css']
})
export class EditAppointmentComponent implements OnInit {
    startDate: string;
    localizeDatePipe: LocalizedDatePipe;

    constructor(
        private translateService: TranslateService
    ) {
        this.localizeDatePipe = new LocalizedDatePipe(this.translateService);
    }

    ngOnInit() {
        this.startDate = this.localizeDatePipe.transform('2021-04-08T07:30:00Z', 'd/M/yyyy, h:mm a');
    }
}