2

I have an Angular project that stores data inside firebase. The data is stored as string (prdName: string;) inside the database. I want to ask if is it possible if i put a html tag inside the string like <b>this is text</b> and store it and then bind/view them as html text format? (the text become bold)

firebase

//service.ts
getData() {
  this.List = this.firebase.list('Product');
  return this.List;
}

insertProduct(Product: Product) {
  this.productList.push({
    prdName: Product.prdName,
    prdCategory: Product.prdCategory,
    prdSup: Product.prdSup,
    prdImage: Product.prdImage,
    prdDescription: Product.prdDescription
  });
}

//component.ts
ngOnInit() {
  var x = this.ListService.getData();
  x.snapshotChanges().subscribe(item => {
    this.List = [];
    item.forEach(element => {
      var y = element.payload.toJSON();
      y["$prdKey"] = element.key;
      this.List.push(y as List);
    });
  });
}
<!--component.html-->
<label>Product Name: </label> {{ListService.selectedProduct.prdName}}

Please let me know if more snippets are needed. Thank you very much in advance.

0

2 Answers 2

1

You have to use innerHtml to bind html tags :

<div [innerHTML]="ListService.selectedProduct.prdName"></div>

Check this : https://angular.io/guide/template-syntax#!#property-binding-or-interpolation-

Sign up to request clarification or add additional context in comments.

Comments

1

I used such pipe in my project to make it work right

import { PipeTransform, Pipe } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser'

@Pipe({ name: 'safeHtml'})
export class SafeHtmlPipe implements PipeTransform  {
  constructor(private sanitized: DomSanitizer) {}

  transform(value) {
    return this.sanitized.bypassSecurityTrustHtml(value);
  }
}

then in the place where you want to have your html you simply do

<div [innerHTML]="someHtmlContent | safeHtml"></div> 

pipe is needed to make this html content trusted, more information about this:

https://angular.io/guide/security#bypass-security-apis

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.