Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a new line in MatDialog Content Angular 7

I am using MatDialog and trying to add a new line in the content definition. Both \n and </b> are not doing it. Is there another way without having to manually go into the html and change it since it's a reusable component:

var status: MatDialogRef<GenericDialogComponent> this.dialog.open(GenericDialogComponent,{
     width: '400px',
    data: {title: "Sample Title?", content: "Document " + this.docID + " has been saved. The users email address is provied below:\n\n"+this.email+"</b>"} });

HTML

<h1 mat-dialog-title>{{data.title}}</h1>
<div mat-dialog-content>
  <p>{{data.content}}</p>
</div>
<div mat-dialog-actions>
  <button mat-button (click)="Cancel()">Cancel</button>
  <button mat-button (click)="Ok()" cdkFocusInitial>Ok</button>
</div>
like image 224
Flash Avatar asked Apr 12 '19 13:04

Flash


4 Answers

You can use the [innerHTML] property:

<p [innerHTML]="data.content"></p>

and instead of \n\n, use the html br tag.

const status: MatDialogRef<GenericDialogComponent> this.dialog.open(GenericDialogComponent,{
     width: '400px',
    data: {title: "Sample Title?", content: `Document ${this.docID} has been saved. The users email address is provied below:<br /><b>${this.email+}</b>`} });
like image 86
Christian Benseler Avatar answered Nov 12 '22 21:11

Christian Benseler


Try to use the <pre> HTML tag if you need/want to use \n.

But I would suggest Christian Benseler's answer !

like image 44
Alex Mougenet Avatar answered Nov 12 '22 20:11

Alex Mougenet


Try:

<div mat-dialog-content [innerHtml]="'<p>' + data.content + '</p>'">

EDIT @Christian Benseler's answer is better/prettier.

like image 2
Tim VN Avatar answered Nov 12 '22 19:11

Tim VN


In case others find it useful when hitting this answer, for my needs, I have copied the configuration of a generic dialog and service from Building a reusable dialog module with Angular Material

And by using the earlier answer where HTML <br/> was set into the string in code, I separated out this by using a Pipe (useful in other display needs too), of course this assumes a dialog is only doing a small amount of data display not showing a whole book chapter ;-)

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

@Pipe({
  name: 'newlineToBR',
})
export class NewlineToBRPipe implements PipeTransform {
  transform(value: string): string {
    return value.replace(/\n/g, '<br/>');
  }
}

in the common dialog component:

<div mat-dialog-content>
      <p class="dialog-message" [innerHTML]="data.message | newlineToBR"></p>
    </div>

then in the component calling the dialog, there is no HTML tags, just make sure to add in newlines in editor and use the correct quote mark type:

const options = {
title: 'Confirm De-select of Review Required',
message: `If you select OK then all the following fields will be reset:
- start date
- assigned person`,
cancelText: 'CANCEL',
confirmText: 'OK',

};

like image 1
rob cairney Avatar answered Nov 12 '22 19:11

rob cairney