How to add background-image using ngStyle (angular2)?

前端 未结 9 2235
青春惊慌失措
青春惊慌失措 2020-11-28 05:36

How to use ngStyle to add background-image? My code doesn\'t work:

this.photo = \'http://dl27.fotosklad.org.ua/20121020/6d0d7b1596285466e8bb06114a88c903.jpg\         


        
9条回答
  •  忘掉有多难
    2020-11-28 05:45

    import {BrowserModule, DomSanitizer} from '@angular/platform-browser'
    
      constructor(private sanitizer:DomSanitizer) {
        this.name = 'Angular!'
        this.backgroundImg = sanitizer.bypassSecurityTrustStyle('url(http://www.freephotos.se/images/photos_medium/white-flower-4.jpg)');
      }
    

    See also

    • https://angular.io/docs/ts/latest/api/platform-browser/index/DomSanitizer-class.html
    • In RC.1 some styles can't be added using binding syntax
    • Plunker example

提交回复
热议问题