Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attribute property binding for background-image url in Angular 2

I have been struggling to figure out the best way to dynamically change the background-image attribute in a number of Angular 2 components.

In the following example, I am attempting to set the background-image of a div to an @Input value using [ngStyle] directive:

import {Component, Input} from '@angular/core'; import { User } from '../models';  // exporting type aliases to enforce better type safety (https://github.com/ngrx/example-app) export type UserInput = User;  @Component({   selector: 'profile-sidenav',   styles: [ `     .profile-image {       background-repeat: no-repeat;       background-position: 50%;       border-radius: 50%;       width: 100px;       height: 100px;     }   `],   template: `       <div class="profile-image" [ngStyle]="{ 'background-image': url({{image}})">     <h3>{{ username }}</h3>   ` })  export class ProfileSidenav {   @Input() user: UserInput;   blankImage: string = '../assets/.../camera.png';    // utilizing "getters" to keep templates clean in 'dumb' components (https://github.com/ngrx/example-app)    get username() {     return this.user.username;   }   get image() {     if (!this.user.image) { return this.cameraImage;     } else { return this.user.image; }   } 

I don't think the issue is with the observable, since username displays and doing something like <img *ngIf="image" src="{{ image }}"> renders the image. I have to access the background-image attribute because apparently that is the best way to make a circular image, but in general would like to know how to do this.

EDIT:

My original [ngStyle] declaration had unnecessary curly brackets (ngStyle is a directive that can take a variable), and was missing string tags around url() and image. The correct way is (as answered below) is:

<div class="profile-image" [ngStyle]="{'background-image': 'url(' + image + ')'}"></div>`. 

As stated in the original edit, a solution can also be achieved with the Renderer class in Angular 2. I have yet to do it but think there should be a way with setElementStylesor something like that. I will try to post an example but would love if someone else showed me (and others) how to for the time being.

like image 390
0xPingo Avatar asked Jun 01 '16 19:06

0xPingo


People also ask

Which property we can define using background-image URL?

The background-image property is used to set one or more background images for an element. By default, it places the image on the top left corner. To specify two or more images, we need to specify the separate URLs with a comma for both images.

How do I add a background-image to my URL?

Usage is simple — you insert the path to the image you want to include in your page inside the brackets of url() , for example: background-image: url('images/my-image. png'); Note about formatting: The quotes around the URL can be either single or double quotes, and they are optional.

Which property is used for background-image?

The background-image CSS property sets one or more background images on an element.

What is attr for in Angular?

Attribute binding in Angular helps you set values for attributes directly. With attribute binding, you can improve accessibility, style your application dynamically, and manage multiple CSS classes or styles simultaneously.


1 Answers

I think that you should use something like that:

<div class="profile-image"      [ngStyle]="{ 'background-image': 'url(' + image + ')'}"> 

where image is a property of your component.

See this question:

  • How to add background-image using ngStyle (angular2)?
like image 155
Thierry Templier Avatar answered Oct 13 '22 23:10

Thierry Templier