-2

I am trying to programmatically change the background image in angular.

this.document.getElementsByClassName("modal-header email-header")[0].style= 'background-image=""

It does not seem to work.

Nesan Mano
  • 1,892
  • 2
  • 26
  • 43
  • It would be better to avoid document query and instead apply it directly on the component. https://stackoverflow.com/a/37575497/15439733 – Joosep Parts Oct 28 '22 at 02:11

1 Answers1

1

In angular we use variables in .ts and bind the variables in the .html. So you can declare a variable in your .ts

background:string="assets/img.jpg"

And in .html

   <div class=""modal-header email-header" 
      [style.background-image]="background">

When in .ts you change the variable (I imagine in a function or in an event)

   this.background=null; //in any place

The "style.background-image" change in .html

NOTE: Instead use [style.background-image], you can also use [ngStyle] or [ngClass]

NOTE2: I always suggest make a tour of Heroes when we start with Angular or beings with Getting started with Angular

Eliseo
  • 50,109
  • 4
  • 29
  • 67