How to use ngStyle for background url in Angular 4

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

I have below html:

  
  • 7条回答
    •  南方客
      南方客 (楼主)
      2020-12-28 22:24

      Working for Angular 8+ (Power of template literals): In component, define ngStyle object var (here called as styles, initialised in constructor):

      const bgImageUrl = 'assets/images/dot-grid.png'
      const styles = {
        backgroundImage: `url(${bgImageUrl})`
      };
      

      In template, assign this as ngStyle

    提交回复
    热议问题