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

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

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

3条回答
  •  爱一瞬间的悲伤
    2021-01-14 02:15

    Thanks to Harsha Sampath. I did:

    [style.backgroundImage]="'url('+urlToImage+')'"
    

    works in Angular 8, urlToImage is a string property of component, resolved at runtime.

    The other settings of background are fixed in css:

    background-repeat: no-repeat;
    background-position:center center;
    background-size: 100% 100%;
    

提交回复
热议问题