Reputation: 23
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!
Upvotes: 0
Views: 10793
Reputation: 3387
You can try [ngStyle]="{'background-image': 'url(' + imageName + ')'}"
Upvotes: 4