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>
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>`} });
Try to use the <pre>
HTML tag if you need/want to use \n
.
But I would suggest Christian Benseler's answer !
Try:
<div mat-dialog-content [innerHtml]="'<p>' + data.content + '</p>'">
EDIT @Christian Benseler's answer is better/prettier.
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',
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With