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