There are many possible approach already mentioned in other answers, Since we need to solve this problem without using ts or js
We can solve it using css and HTML only
Approach here is despite of adding ngIf to load different image we can use css to load different image
<body class="dark">
<img class="image-replacement" />
</body>
.dark .image-replacement {
display: block;
-moz-box-sizing: border-box;
box-sizing: border-box;
background: url(http://akamaicovers.oreilly.com/images/9780596517748/cat.gif) no-repeat;
width: 180px;
height: 236px;
padding-left: 180px;
}
.light .image-replacement {
display: block;
-moz-box-sizing: border-box;
box-sizing: border-box;
background: url(http://ashlandtech.org/wp-content/uploads/2014/09/1.png) no-repeat;
width: 180px;
height: 236px;
padding-left: 180px;
}
Please find Working fiddle linkhttps://jsfiddle.net/vickykumarui/4po9hu6g/
As far as your code is concerned it is correct
There may be one reason why it is not working is because document is not injected in your angular component
Once you inject document in your angular component it will start working
import { DOCUMENT } from '@angular/common';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
constructor(@Inject(DOCUMENT) private document: Document) {}
}
Please find working demo of the approach you followed
https://stackblitz.com/edit/angular-qighyt