How to use ngStyle for background url in Angular 4

后端 未结 7 642
逝去的感伤
逝去的感伤 2020-12-28 21:56

I have below html:

  
  • 7条回答
    •  囚心锁ツ
      2020-12-28 22:12

      If you're getting your background image from a remote source or a user input you will need to have angular sanitize the url. In your component you will want to add the following bits of code...

      import { DomSanitizer } from '@angular/platform-browser';
      
      export class YourComponent {
        constructor(private _sanitizer: DomSanitizer) { }
      
        public sanitizeImage(image: string) {
          return this._sanitizer.bypassSecurityTrustStyle(`url(${image})`);
        }
      }
      

      Try setting your HTML to this...

    • Make your food with Spicy.

    • And apply the no-repeat 0px 0px; in some class you attach to the div.

    提交回复
    热议问题