在我的Angular应用程序中,我试图显示完整文件路径中的文件名。
<ng-template kendoGridCellTemplate let-dataItem="dataItem">
<a style="text-decoration: underline !Important;" href="javascript:void(0)" (click)="downloadFile(dataItem)">{{dataItem.file.replace(/^.*[\\\/]/, '')}}</a>
</ng-template>
dataItem.file.replace(/^.*[\\\/]/, '')
在组件类中使用时有效,但在带插值的html中失败。
我收到以下错误消息:
Parser Error: Unexpected token / at column 35 in [{{dataItem.file.replace(/^.*[\\\], '')}}] in ng:///AppModule/MyComponent.html@24:130 ("-decoration: underline !Important;" href="javascript:void(0)" (click)="downloadFile(dataItem)">[ERROR ->]{{dataItem.file.replace(/^.*[\\\], '')}}</a>
插值中的这行有什么问题吗?有指针吗?
{{dataItem.file.replace(/^.*[\\\/]/, '')}}
答案 0 :(得分:1)
在以下插值中,您将创建一个RegExp
对象,该对象在该上下文中是不允许的:
{{ dataItem.file.replace(/^.*[\\\/]/, '') }}
您可以将调用包装在组件的方法中:
formatPath(path: string): string {
return path.replace(/^.*[\\\/]/, '');
}
并在模板中调用方法:
{{ formatPath(dataItem.file) }}