I need to change the format to display an XML on the screen in Angular 2, and I found a script that works.
I call said format-xml
through a Pipe
:
import { Pipe, PipeTransform } from '@angular/core';
import * as jQuery from 'jquery';
import { escape } from 'querystring';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
//import * as angular from '../angular.js';
@Pipe({
name: 'formatXml'
})
export class FormatXmlPipe implements PipeTransform {
domSanitizer: DomSanitizer;
transform(xml: string): string {
var formatted = '';
var reg = /(>)(<)(\/*)/g;
xml = xml.replace(reg, '$1\r\n$2$3');
var pad = 0;
jQuery.each(xml.split('\r\n'), function (index, node) {
var indent = 0;
if (node.match(/.+<\/\w[^>]*>$/)) {
indent = 0;
} else if (node.match(/^<\/\w/)) {
if (pad != 0) {
pad -= 1;
}
} else if (node.match(/^<\w[^>]*[^\/]>.*$/)) {
indent = 1;
} else {
indent = 0;
}
var padding = '';
for (var i = 0; i < pad; i++) {
padding += ' ';
}
formatted += padding + node + '\r\n';
pad += indent;
});
var escaped = formatted.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/ /g, ' ').replace(/\n/g, '<br />');
/*
https://stackoverflow.com/questions/39240236/insert-xml-into-dom-in-angular-2
escaped = this.domSanitizer.bypassSecurityTrustHtml(escaped);
*/
//this.trusted();
return escaped;
}
directly using the HTML file of my Component:
<td *ngIf="i==_rowToShow" >{{car.ID| formatXml}}</td>
The call is successful, and it works.
My problem comes when I "print it" on the screen.
As I read, it has to do with HTML
, and I need to use Safe-Html
. I found a example , but I'm not sure how to use it, since that the string
I get from the Pipe, and the template from HTML