0

i want to change the background-image from the Typescript file in Agular, i tried but don't work.

I change the style directly in my html file because i don't know how to do in the css.

This is the line in my html file:

<div style="background-image: url({{imageName}});"> </div>

and this is my Typescript file:

export class NAmeComponent implements OnInit {

  imageName: String;

ngOninit(){
this.getImage();
}

getImage(){
 switch (value) {
      case 'a': {
        this.imageName = 'Nebula.jpg';
        break;
      }
     case 'b': {
        this.imageName = 'Eclipse.jpeg';
        break;
      }
    }
}
}

How can i fix this? Thank all for the answer!

2
  • try [style.background-image]="'url(' + imageName + ')'" Commented Mar 1, 2019 at 12:24
  • you can use something like <img [src]='yourVariableHere' /> Commented Mar 1, 2019 at 12:24

1 Answer 1

4

You can try [ngStyle]="{'background-image': 'url(' + imageName + ')'}"

Sign up to request clarification or add additional context in comments.

1 Comment

i fight with this problem from 2 days XD

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.