I have a nav-menu component and this component is the same for all the pages except I want the image property to change. What I mean is that I want different background images for different pages. For example I want the red car to be for the contact page, the grey car for the about page. How can I achieve that? Here is my code:
<div>
<nav class="navbar navbar-expand-md navbar-dark bg-dark">
<a class="navbar-brand" href="#"></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="container">
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav text-uppercase">
<li class="nav-item">
<a class="nav-link" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Services</a>
</li>
<li class="nav-item">
<a routerLink="/about" class="nav-link">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Reviews</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Locations</a>
</li>
<li class="nav-item">
<a routerLink="/contact" class="nav-link">Contacts</a>
</li>
</ul>
</div>
</div>
</nav>
<img [src]="blackcar" alt="blackcar" class="black-car">
</div>
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-nav-menu',
templateUrl: './nav-menu.component.html',
styleUrls: ['./nav-menu.component.scss']
})
export class NavMenuComponent implements OnInit {
blackcar = "assets/images/blackcar.png";
redcar = "assets/images/redcar.png";
greycar = "assets/images/greycar.png";
constructor() { }
ngOnInit(): void {
}
}
The cleanest way to do this would be to use css to load the images as backgrounds. Then the background displayed is based on some condition that will result on one of the enumerations of
background-type.ts
orBackgroundType
, so your solution should look like something between these lines:nav-menu.component.scss:
background-type.ts
nav-menu.component.ts
nav-menu.component.html