1

In my Angular application, I am trying to display file name from complete file path.

<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(/^.*[\\\/]/, '') works when I use in component class but fails in html with interpolation.

I get error as:

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>

Is there anything wrong with this line in the interpolation ? Any pointers ?

{{dataItem.file.replace(/^.*[\\\/]/, '')}}

1 Answer 1

1

In the following interpolation, you are creating a RegExp object, which is not allowed in that context:

{{ dataItem.file.replace(/^.*[\\\/]/, '') }}

You can wrap the call in a method of the component:

formatPath(path: string): string {
  return path.replace(/^.*[\\\/]/, '');
}

and call the method in the template:

{{ formatPath(dataItem.file) }}
Sign up to request clarification or add additional context in comments.

1 Comment

This is Great. Thanks @ConnorsFan

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.