英文:
String replace to html tag in Angular 13
问题
我是一个初级的Angular开发者。
我有以下代码:
export class KoniecComponent implements OnInit {
  // ...
  wiadomosc: string;
  urlView: string;
  constructor(
    private cService: CService
  ) { }
  ngOnInit(): void {
    this.registerC();
  }
  registerC(): void {
    this.cService.saveC(this.szkoda).subscribe(
      wynik => {
       
        const json = JSON.stringify(wynik);
        let txt = JSON.parse(json);
        if(txt.result_info != "" && txt.result_info != null)
        {
          let msg = txt.result_info;
          let url = txt.pm;
          this.urlView = txt.pm;
          this.wiadomosc = msg.replace(txt.number, '<b>'+txt.number+'</b>');
        }
        else {
          ...............
        }
      },
      error => {
        .......
      }
    );
  }
}
我在这一行遇到了问题:
this.wiadomosc = msg.replace(txt.number, '<b>'+txt.number+'</b>');
结果中我得到了 <b> 标签 - 文本没有加粗。
如何修复它?请帮助我。
英文:
I am beginner Angular developer.
i have this code:
export class KoniecComponent implements OnInit {
  ....
  wiadomosc: string;
  urlView: string;
  constructor(
    private cService: CService
  ) { }
  ngOnInit(): void {
    this.registerC();
  }
  registerC(): void {
    this.cService.saveC(this.szkoda).subscribe(
      wynik => {
       
        const json = JSON.stringify(wynik);
        let txt = JSON.parse(json);
        if(txt.result_info != "" && txt.result_info != null)
        {
          let msg = txt.result_info;
          let url = txt.pm;
          this.urlView = txt.pm;
          this.wiadomosc = msg.replace(txt.number, '<b>'+txt.number+'</b>');
        }
        else {
          ...............
        }
      },
      error => {
        .......
      }
    );
  }
}
I have problem with this line:
this.wiadomosc = msg.replace(txt.number, '<b>'+txt.number+'</b>');
In result i have 'b' tabs - not bolded text.
How can i repeir it? Please help me
答案1
得分: 0
在这里你可以像这样使用。
this.wiadomosc = msg.replace(txt.number, ''<b>{{txt.number}}</b>');
英文:
Here you can use like this.
this.wiadomosc = msg.replace(txt.number, '<b>{{txt.number}}</b>');
答案2
得分: 0
如果你想在Angular中加载富HTML文本,那么插值将不起作用,你需要将其设置在innerHTML属性内,像这样。
<span [innerHTML]="wiadomosc"></span> 这将加载 <b>test</b> test 作为DOM中的粗体。
英文:
If you want to load rich html text in angular then interpolation will not work
you need to set it inside innerHTML attribute like this.
<span [innerHTML]="wiadomosc"></span> this will load <b>test</b> test as bold in DOM
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论