What is the recommended way to dynamically set background image in Angular 4

后端 未结 3 1338
野性不改
野性不改 2021-01-14 01:40

Currently I\'m setting the image background using inline-style.

3条回答
  •  无人及你
    2021-01-14 02:07

    You can't do it thourgh CSS files. However, you can make your current code little less verbose :

    If angular throws security errors, you will have to sanitize too. So you will do something like this: in HTML file-

    in your TS file

    this.background= 
    this.sanitization.bypassSecurityTrustStyle(`url(${this.section.backgroundSrc}) no-repeat`);
    

提交回复
热议问题