0

I want to make notification bar and have HTML template as this one:

<ul>
  <li *ngFor="let notification of notifications">
    <div class="message"> ... </div>
  </li>
</ul>

But for different notifications, there should be different message templates, like these:

<div class="message"> <b>NEW</b> message arrived </div> or
<div class="message"> Message is <b>DELETED</b> </div> or
<div class="message"> Your message is <b>SENT</b> </div> etc...

I could make an ugly method in component to print message with these HTML tags, but is there more elegant way to this?

ugly method:

resolveMessage(status) {
  if (status == 'new') {
    return '<b>NEW</b> message arrived';
  } else if (status == 'sent') {
    return 'Your message is <b>SENT</b>';
  }
}

<div class="message"> {{ resolveMessage(notification.status) }} </div>
milosdju
  • 783
  • 12
  • 27

2 Answers2

3

components.ts

messages = {
  new: `<b>NEW</b> message arrived`,
  sent: `Your message is <b>SENT</b>`,
  deleted: `Message is <b>DELETED</b>`
}

template.html

<li *ngFor="let notification of notifications">
  <div class="message" [innerHTML]="messages[notification.status]"></div>
</li>

Ng-run Example

yurzui
  • 205,937
  • 32
  • 433
  • 399
1

You can use *ngIf based on the status display the messages

<li *ngFor="let notification of notifications">      
    <div *ngIf="notification.status ==='new'" class="message"> NEW  message arrived</div>
</li>
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396