Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 利用角度插值中的导入函数_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 利用角度插值中的导入函数

Javascript 利用角度插值中的导入函数,javascript,angular,typescript,Javascript,Angular,Typescript,我在下面的组件中尝试使用我创建的名为FormatDate的函数,该函数以特定方式显示日期。我本来希望在html中通过插值实现它,但是该函数无法识别。有没有一种方法可以在不将其包装到组件本地函数的情况下使用它?我把文件通读了一遍,但什么也没读出来 import { Component } from '@angular/core'; import { FormatDate } from '../shared/commonFunctions'; @Component({ selector: '

我在下面的组件中尝试使用我创建的名为FormatDate的函数,该函数以特定方式显示日期。我本来希望在html中通过插值实现它,但是该函数无法识别。有没有一种方法可以在不将其包装到组件本地函数的情况下使用它?我把文件通读了一遍,但什么也没读出来

import { Component } from '@angular/core';
import { FormatDate } from '../shared/commonFunctions';
@Component({
    selector: 'my-component',
    template: '<h3>{{FormatDate(rightNow)}}</h3',
})
export class myComponent {
    rightNow = Date();
}
我的解决方法如下,但对我来说很难看,所以我希望能找到一种解决方法

import { Component } from '@angular/core';
import { FormatDate } from '../shared/commonFunctions';
@Component({
    selector: 'my-component',
    template: '<h3>{{dateFormat(rightNow)}}</h3',
})
export class myComponent {
    rightNow = Date();

    dateFormat(date: string): string {
        return FormatDate(date);
    }
}
别忘了将新管道添加到模块声明数组中。

我认为角形管道是您需要的。
import {Pipe, PipeTransform} from "@angular/core";
@Pipe({name: 'transformDatePipe'})
export class TransformDatePipe implements PipeTransform {

    transform(date: string, params:any...): any {
        return yourFunction();
    }
}
import { Component } from '@angular/core';
import { FormatDate } from '../shared/commonFunctions';
@Component({
    selector: 'my-component',
    template: '<h3>{{rightNow|transformDatePipe:anyAdditionalParamsYouNeed}}</h3',
})
export class myComponent {
    rightNow = Date();
}