Background image covering entire page is exclusive for the home/index page only

I'm facing an interesting dilemma.. I've built a single-page application with a layout that includes a header, footer, and uses ui-router to display views.

Now, my challenge is to have a full-page background on the homepage (index.html) and a white background on all other pages. How can I achieve this?

Here's a snippet of the HTML code:

<body ng-controller="MainController">

    <header>            
        <nav>
            <ul>
                <li><button>Login</button></li>
                <li><button>Tell Me More!</button></li>
            </ul>
        </nav>
    </header>


    <main>
        <section class="container">
            <div ui-view /></div>
        </section>
    </main>

    <footer>
            <ul>
                <li>Contact Us</li>
                <li>About Us</li>
            </ul>
    </footer>


    <script src="/assets/js/vendor/angular.min.js"></script>
    <script src="/assets/js/vendor/angular-ui-router.js"></script>
    <script src="/assets/js/vendor/angular-animate.js"></script>
    <script src="/assets/js/all.min.js"></script> 

</body>

And here's a piece of the app.js file:

var app = angular.module('myApp', ['ui.router', 'ngAnimate']);

app.config(function($stateProvider, $urlRouterProvider) {

    $stateProvider

        .state('index', {
            url: '/index',
            templateUrl: 'partial/home.html',
            controller: 'HomeController'
        })

        .state('login', {
            url: '/login',
            templateUrl: 'partial/login.html',
            controller: 'LoginController'
        });

        $urlRouterProvider.otherwise('/index');
});

Answer №1

To achieve different styles or images for various pages, you can establish $rootScope variables that will be passed down to the controllers' scopes or transmit styling parameters to different states via UIrouter. These settings can then be utilized in the views to apply the desired styles or images.

Important to note: When users navigate to the homepage, they are not directly on index.html, but rather in the state that you have designated as the homepage (as assumed in your initial inquiry).

Here are a few examples:

Option 1 - Setting variables in $rootScope

Within your module's app.run(...), you can listen to state change events and define specific variables for your routes, such as:

angular.module('yourModuleNameHere').run(function($rootScope, ...
 // Global settings:
 $rootScope.isHome = false;
 $rootScope.hasInnerHeader = false;
 $rootScope.bodyClass = "";
 $rootScope.bodyStyle = "";

 // Page-specific settings on route change
 $rootScope.$on("$stateChangeSuccess", function(event, currentRoute, previousRoute) {
  // Other processing may go here 
  switch ($state.current.name) {
        case 'main.home':
            $rootScope.headerClass = 'home-header';
            $rootScope.isHome = true;
            $rootScope.bodyClass = "home-body background-full";                
            break;
        default:
            // Default settings
 }

These settings can be directly used in your partial files:

<div id="header" class="{{headerClass}}">...header content...</div>

Since the partial scope inherits from $rootScope.

Option 2 - Passing parameters from UIrouter (which can be retrieved from a configuration file or injected as a provider in your app.config)

Define page-specific data for your route:

$stateProvider
    .state('index', {
        url: '/index',
        templateUrl: 'partial/home.html',
        controller: 'HomeController',
        pageSettings: {
            bodyClass: "homepage-class";
        }
    })

In the partial controller, you can access this data like so:

Make sure to inject the dependency for $state

function _init() {
    // ***** Check Route params *******
    if ($state.current.pageSettings && $state.current.pageSettings.bodyClass)   {
     // Perform actions with the class, such as assigning it to the scope:
     $scope.myPartialClass = $state.current.pageSettings.bodyClass

     // Or apply different settings based on the class
        switch ($state.current.pageSettings.bodyClass) {
            case "homepage-class":
                $scope.hideSomeDiv = true;
                $scope.anotherSetting = false;
                break;
            case "homepage-fullscreen-class":
                $scope.hideSomeDiv = false;
                break;
            default:
        }
    }
}

_init();// Call the initialization method

PROs: These methods enable you to have distinct styles for each page, modify layout options, and customize settings from a configuration file.

CONs: It may require a bit more effort, especially if styling is only needed for a single page.

Note: The code provided is for reference and has not been tested, but should give you a general idea of the implementation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JQuery datepicker is functioning properly only after an alert when loaded with AJAX

Here's a puzzling question I have encountered. I implemented a field with a datepicker functionality. This field gets loaded into a div through an AJAX call. To sum it up, everything seems to be in working order.. But there's a strange issue. I ...

JSON object name

Here are the specific file locations for loading each of the CSS and JS files. <link href="css/default.css" rel="stylesheet" /> <script src="js/main.js"></script> In XML, the filename is input as shown below ...

Challenges encountered when trying to incorporate vanilla JavaScript within a Ruby environment

While working on my controller, I attempted to render vanilla JavaScript but encountered an unexpected outcome. Below is the code snippet: class UploadController < ApplicationController def index render :file => 'app\views&bs ...

When I click a button in d3 to refresh the data on my bar graph, the text fails to update accordingly

I've successfully created a series of data lists that modify the bargraph. Unfortunately, due to their differing x and y values, they end up printing new values on top of existing ones. Shown below is an image illustrating the issue where x and y val ...

The functionality of ng-click and ng-submit seems to be malfunctioning

I am currently facing an issue with my Angular application and PhoneGap. I have a login form along with a login controller set up, but for some reason, the ng-submit function is not working as expected. When the submit button calls the formConnexion() func ...

The out directory is lacking Styled JSX integration for Next.js

I have a straightforward project in NextJs and I am looking to serve the files via NginX. Here are the dependencies listed in my package.json file: "dependencies": { "isomorphic-unfetch": "^2.0.0", "next": "^7.0.2", "next-routes": "^1.4.2", ...

How can I iterate through multiple rows in JavaScript?

Feeling stuck, the simple yet dreaded for loop has become my nemesis and I could really use some guidance. Currently, I have a Google sheet with 3 rows (excluding headers) and 8 columns. As users input data via a web app, the number of rows will dynamicall ...

Centering text using CSS Bootstrap

Hey everyone, I've been attempting to center my text using Bootstrap, but for some reason it's not working. Can someone help me figure out what's going on? <footer class="row p-1 mt-3 bg-primary text-white"> <p class= ...

Creating interactive lists on Android devices with PhoneGap and jQuery Mobile

I am facing an issue with my code. It works perfectly in Chrome, but when I try to run it on Android, the alert("test") function is never called. I created an apk using PhoneGap and am also using jQuery Mobile. Can anyone help me figure out what I am missi ...

Using React to Filter cards according to the selected value from an Autocomplete feature

I'm currently developing a React application that utilizes Material-UI's Autocomplete component to filter cards based on selected car brands. These cards represent various models, and the goal is to update them when a user picks a brand from the ...

Wondering how to go back to the default locale in Next.js?

Within my Next.js application, I have successfully implemented the next-i18next module for multi-language support. The app currently supports two languages: English and Arabic, with English set as the default. To allow users to toggle between languages, I ...

Tips for utilizing flexbox to establish a maximum width and prevent abrupt layout changes when incorporating media queries

I am facing an issue with my responsive design using flexbox. On mobile, my 3 boxes containing products named Ragnar, Thor, and Odin look great. However, when I switch to tablet or desktop view, the boxes suddenly grow larger due to the media query set wit ...

A guide on smoothly navigating to the desired row in a gridview using Asp.net

Currently, I am developing a project using ASP.net and C# technology. In my application, I integrated a GridView that contains multiple rows. Within the grid, there is a text box and a search button available. The columns displayed in the grid are Id and ...

The properties of margin:auto and text-align:center are failing to function as expected

I'm facing an issue while creating a website. The 'margin: auto' and 'text-align: center' properties in my CSS code seem to not be functioning as expected. Can someone please review my code in inspect element and identify the probl ...

Numerous identifiers and a distinct title

As a beginner in Ruby and JavaScript, I am unsure of my actions. I have a dropdown box that I want to display by unique name. Using JavaScript (CoffeeScript), I retrieve results with JSON. I have implemented a method to display by unique name, which is f ...

Unable to shift the header menus upwards for proper alignment with the logo

Header with logo I'm trying to align the menu items like Home, etc with the logo, but it seems like the logo is taking up too much space above the menu headers and I'm not sure how to reduce it. I've already attempted using margin-left or r ...

Can you explain what findDOMNode is and why it is no longer supported in StrictMode within the console?

I attempted to create a count-up feature using React visibility sensor and React count up, but encountered an error in the console. Is there a correct solution to this issue? Caution: The use of findDOMNode is deprecated in StrictMode. This method was uti ...

Include a tab button within a vertical tab list using Angular Material

I have utilized Angular Material to create a vertical tab, and I would like to incorporate an Add Tab button within the tab listing itself. Currently, when I add the button, it appears at the bottom of the layout instead. For reference, you can access the ...

Tooltips remain inactive on the first plot until there is a change in the plot, triggering their functionality

I've encountered a strange issue with tooltips using d3-tip in a scatter plot. Initially, the tooltips are not functioning at all, and even the mouseover events are not triggering as expected. The scatter plot consists of three sets of data on each a ...

Use setTimeout and setInterval with the initial input without parentheses or double quotation marks

<!DOCTYPE HTML> <html> <head> <script type="text/javascript"> var count=0; function increaseCount(){ document.getElementById('txt').value=count; count++; setTimeout(increaseCount(),1000); } </script> </head&g ...