0

I am new to angular. I used pipe to format date. I used following but, didn't work.

<tr *ngFor="let item of items; index as i" [attr.data-rel]="'sometext-' + item.startTime | dateFormat ">

If i remove either 'timeslot-' + or | dateFormat it works but, with string concatenation it doesn't work.

Following works.

<tr *ngFor="let item of items; index as i" [attr.data-rel]="'sometext-' + item.startTime">

Or

<tr *ngFor="let item of items; index as i" [attr.data-rel]="item.startTime | dateFormat ">
5
  • what is the expected result? someText - (dateformated) or (sometext-date)formated? Commented Mar 28, 2019 at 15:48
  • @cucuru expected result: someText-dateformatted Commented Mar 28, 2019 at 15:50
  • 3
    <tr *ngFor="let item of items; index as i" [attr.data-rel]="'sometext-' + (item.startTime | dateFormat )"> Commented Mar 28, 2019 at 15:51
  • 1
    wrap (item.startTime | dateFormat ) between parenthesis Commented Mar 28, 2019 at 15:51
  • @NadhirFalta Cool. It works please post it as answer. Thank you. :) Commented Mar 28, 2019 at 15:52

2 Answers 2

3

wrap (item.startTime | dateFormat ) between parenthesis

<tr *ngFor="let item of items; index as i" [attr.data-rel]="'sometext-' + (item.startTime | dateFormat )">

Sign up to request clarification or add additional context in comments.

Comments

1

Create a custom pipe

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'dateFormat' })
export class DateFormat implements PipeTransform {
  transform(date: Date, text: string): string {
    return text + date.toString();
  }
}

call it like this

<tr *ngFor="let item of items; index as i" [attr.data-rel]="item.startTime | dateFormat : 'sometext'  ">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.