0

I am printing an XML on my template, and I want to format it. I have found a file to do this task and it works.

For using it, I have created a pipe file:

Pipe

import { Pipe, PipeTransform } from '@angular/core';
import * as jQuery from 'jquery';
import { escape } from 'querystring';

@Pipe({
  name: 'formatXml'
})
export class FormatXmlPipe implements PipeTransform {

  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, '&amp;').replace(/</g, '<').replace(/>/g, '&gt;').replace(/ /g, '&nbsp;').replace(/\n/g, '<br />');
    //this.trusted(escaped);
    return escaped;
  }

HTML

<td *ngIf="i==_rowToShow">{{car.ID| formatXml}}</td>

and it also works, but when in print it in the HTML it does not change.

I think that my problem is that I do not have a safe-html.

I have found a link that explain how to do it here but I am not able to do it. My HTML template is already created. How to I send it as parameter to my Pipe?

gyc
  • 4,300
  • 5
  • 32
  • 54
bellotas
  • 2,349
  • 8
  • 29
  • 60

0 Answers0