1

how to set a background image for a particular component of angular? Not for the whole app. How can I achieve that. I have seen index.html implementations but it goes for the whole app

<div class="navbar navbar-default">
  <div class="container">
    <div id="navbar-main">
      <ul class="nav navbar-nav">
        <li><a routerLink="/">Home</a></li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li *ngIf="!auth.isLoggedIn()"><a routerLink="/login">Sign in</a></li>
        <li *ngIf="!auth.isLoggedIn()"><a routerLink="/register">Sign up</a></li>
        <li *ngIf="auth.isLoggedIn()">
          <a routerLink="/profile">{{ auth.getUserDetails()?.name }}</a>
        </li>
        <li *ngIf="auth.isLoggedIn()"><a (click)="auth.logout()">Logout</a></li>
      </ul>
    </div>
  </div>
</div>

I want to set background image for router-outlet component

<router-outlet></router-outlet>
4
  • Is this the component you need the background on? Commented May 31, 2018 at 15:18
  • yes this the main component in which router-outlet refers to home component indirectly i have to set background image on home component which will come below navigation Commented May 31, 2018 at 15:21
  • I believe you need to set it on the component you are loading into router outlet, not the home component. For example, the login component or register. Commented May 31, 2018 at 15:22
  • on main component what i have to write can you tell me Commented May 31, 2018 at 15:29

1 Answer 1

1

You can try to enclose all your content in a div that will holds all your content on each component that you wanted to have a background and set its background:url(''),. For example Inventory component and Order Component.

In Inventory and Order Component, create a div which holds the content.

<div class="inventory-body">
Some Contents Here...
</div> 

Then in Inventory and Order Component css set its background

.inventory-body{
  color:white;
  padding: 20px;
  background: url('image/directory');
}

Please see this link for live sample code. https://stackblitz.com/edit/so-answer-ng-routebg

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

Comments

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.