How to use ngStyle for background url in Angular 4

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

I have below html:

  
  • 相关标签:
  • 7条回答
    • 2020-12-28 22:11

      The correct answer is [style.background-image]="'url(' + article.uri + ')'"

      but if you are using ngFor for carousel, make sure you have added class 'active' properly.

      This code will NOT working:

      <div class="carousel-item active"
          *ngFor="let article of arr;"
          [style.background-image]="'url(' + article.uri + ')'"></div>
      

      You should use 'active' class for first item only!

      <div class="carousel-item" 
          *ngFor="let article of arr; let i = index;"
          [ngClass]="{'active': i === 0}"
          [style.background-image]="'url(' + article.uri + ')'"></div>
      

      Hope it saves time for someone

      0 讨论(0)
    • 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...

      <li *ngFor="let article of arr;let i=index;">
         <div  *ngIf="i == 0" class="w3l_banner_nav_right_banner" [style.background-image]="sanitizeImage(article.uri)">
           <h3>Make your <span>food</span> with Spicy.</h3>
                  <div class="more">
                      <a href="products.html" class="button--saqui button--round-l button--text-thick" data-text="Shop now">Shop now1</a>
                  </div>
          </div>
      </li>
      

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

      0 讨论(0)
    • 2020-12-28 22:12

      you can use it by adding url path in a single variable. for example

      bgImageVariable="www.domain.com/path/img.jpg";
      
      [ngStyle]="{'background-image': 'url(' + bgImageVariable + ')'}"
      
      0 讨论(0)
    • 2020-12-28 22:13

      This works fine for me:

      js file:

      path = 'url(../assets/about.png)';
      

      array:

      string[] = [this.path];
      

      and HTML file:

      <div *ngFor="let item of array" [ngStyle]="{'background-image': item}">
      
      0 讨论(0)
    • 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

      <div [ngStyle]="styles"><!-- your content --></div>
      
      0 讨论(0)
    • To solve this, the correct syntax is

      <div class="modalContainer" 
        ng-style="{'background-image': 'url(' + selectedMeal.url + ')'}">
      

      Mark answer if it helps.

      0 讨论(0)
    提交回复
    热议问题