Incorporate dynamic body color changes across all pages using AngularJS

On the home page, I want to change the color scheme so that it is consistent across all pages. The user should be able to choose a color from a list on the home page and have it apply to every page. Currently, the color selection only applies to the home page, but I need it to carry over to other pages as well.

I have already implemented a dropdown menu for selecting colors on the home page. However, when I navigate to another page like home1.html using a hyperlink, I want the same color to be displayed there too. How can I achieve this functionality?

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="https://www.w3schools.com/lib/w3.js"></script>
</head>

<body ng-app="myApp" >
    <div ng-controller="myCtrl">
        <h4>
            <small>color box</small>
        </h4>
        <select ng-model="color">
            <option value="">--- Select a color ---</option>
            <option value="{{c}}" style="background-color:{{c}}"
                    ng-repeat="c in colors">
                {{c}}
            </option>
        </select>
        <p>Selected color: <b style="color:{{color}}">{{color}}</b></p>
        <div style="background-color:{{color}}">

           <div class="square" style="background-color:{{color}}">

    <a href="home1.html">Visit our page </a>
    </div>

    </div>

    </div>
</body>

<script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function ($scope) {
        $scope.colors = ['Blue', 'Cyan', 'Green', 'Orange', 'Pink', 'Purple', 'Red'];
        $scope.objects = [
            {id: 1, color: 'Blue'},
            {id: 2, color: 'Cyan'},
            {id: 3, color: 'Green'},
            {id: 4, color: 'Orange'},
            {id: 5, color: 'Pink'},
            {id: 6, color: 'Purple'},
            {id: 7, color: 'Red'}
        ];

    });
</script>

<style>
    p { background-color: LightGray; }

    .blue   { background-color: Blue; }
    .cyan   { background-color: Cyan; }
    .green  { background-color: Green; }
    .orange { background-color: Orange; }
    .pink   { background-color: Pink; }
    .purple { background-color: Purple; }
    .red    { background-color: Red; }


    .square {
        background: #000;
        width: 20vw;
        height: 10vw;
    }
    .square h1 {
        color: #fff;
    }
</style>

Answer №1

To ensure consistency across your application, establish a color variable in the $rootscope.color entity and assign it as the ng-model for your dropdown menu. This will allow you to access the color variable from any state or page within your application.

Answer №2

  • For single-page applications, try setting a color variable such as $rootscope.color which can be accessed from different pages.
  • In the case of multiple-page applications, in order to store color data and access it across various pages, consider using client-side storage options like cookies, localStorage, etc.

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

What is the process for calculating the total sum of input values utilizing JavaScript?

My JavaScript skills are not perfect, and I'm struggling to calculate the total sum of values in the amount input boxes without refreshing the page. Can someone assist me with this challenge? Thank you. function Calculat ...

Uncover the hidden message in a string encoded for JavaScript

This encoded HTML code is intended for use in a JavaScript function <div class=\"ProductDetail\"><div style=\"width:780px\">\r\n\t<div class=\"baslik\" style=\"margin: 0px; padding: 5px 10px ...

JavaScript date selector allowing the selection of multiple dates

As a beginner in JavaScript, I am struggling to create a datepicker input field that allows multiple selections. Despite researching for solutions, none seem to fit my specific case. Can anyone offer guidance on how to achieve this? Your help is greatly ap ...

Secure your AngularJS application with Spring Security for AJAX requests and receive JSON response for authentication

Secure Your Spring Application <security:form-login login-page="/login" default-target-url="/myapp" always-use-default-target="true" authentication-success-handler-ref="myAuthenticationSuccessHandler" authentication-failure-han ...

Tips on saving this information in a MySQL database

I'm currently developing a php script that collects and saves links in mysql. However, I've encountered an issue where the script sometimes captures multiple links for the same download, as shown below: http://www.fileserve.com/file/XXXXXX http: ...

Combining several position-aligned classes into a single div instead of each having their own individual div

http://jsfiddle.net/58arV/ Trying to create a question component where users can choose the correct answer out of 4 options. I planned to use 4 input styles with a checkmark "radio" DIV on the right. When a user clicks on an option, it should display a c ...

Identifying HTML attributes for Web Scraping Techniques

I'm currently working on developing a web scraper and facing challenges in identifying element attributes. My approach involves scanning through the code like moving a "scanning head" from left to right, searching for specific character strings such ...

What is the most effective way to assign multiple functions to the onClick event of a button, based on a specific property,

I have a special button that generates a specific type of code when rendered: <button>{this.props.text}</button> This button is named ButtonCustom. In the render method of the main container, I am trying to achieve the following: function my ...

Looking to populate my HTML fields with values

When attempting to edit, the id is being passed but the data fails to populate in the HTML fields. Please refer to this image. Index @{ ViewBag.Title = "AngularJs Tutorial"; } <style> </style> <h2>AngularJs Tutorial : CRUD operatio ...

What steps should be taken to address IE6 problems when a website functions properly in other browsers?

If you come across a website that functions perfectly on all browsers except for IE6, where the layout is extremely distorted but rebuilding the entire markup is not an option. Furthermore, only CSS selectors supported by IE6 are being utilized on the sit ...

How to Populate Object Literal with Multiple Objects in AngularJS

When I click on "Evan", I receive the following response: {"id":1,"name":"Evan"} If I click on "Robert", I will get: {"id":2,"name":"Robert"} Is there a way to modify this code so that it follows the aforementioned steps and generates an object similar ...

Minimize or conceal iframe

This iframe contains a Google form that cannot be edited. I am looking for a way to close or hide this iframe, either through a button, a popup window button, or without any button at all. The $gLink variable holds the Google form link through a PHP sessio ...

Tips on enlarging a webpage without showing the scroll bar on the main component

My goal is to create a webpage that resembles a window application. However, I've encountered an issue where zooming in on the page results in a scroll bar appearing on the main page. The parameters for the main page (the parent) are set as follows: w ...

Tips for synchronizing JavaScript rendering time with Python requests?

My goal is to retrieve the HTML content of a JavaScript-generated website so that I can extract information using BeautifulSoup. However, when I attempt to request the HTML, the data I receive is from before the page fully loads. Below is the code snippet ...

Pass an image into an input field with the attribute type="filename" using JavaScript directly

My goal is to automate the process of uploading images by passing files directly to an input type="filename" control element using JavaScript. This way, I can avoid manually clicking [browse] and searching for a file in the BROWSE FOR FILES dialog. The re ...

Having issues making div elements with javascript

I am having trouble generating new divs when a specific div is clicked. Despite my efforts, nothing seems to be happening and the console isn't showing any errors. I've searched online for solutions but haven't found anything that addresses ...

Is it feasible to verify for vacant dates with a single click?

Is there a way to determine if a date value is empty, and if it is, display a popup indicating so? After some research, I stumbled upon a similar issue where the date value was always filled with a default "mm/dd/yyyy" value. The solution provided involv ...

Tabs within the AutoComplete popup in Material-UI

Would it be feasible to showcase the corresponding data in the AutoComplete popover using Tabs? There could be potentially up to three categories of data that match the input value, and my preference would be to present them as tabs. Is there a way to in ...

Trouble retrieving data through AJAX request on Intel XDK

I am in the process of developing an APK for the blood bank in my local city. One of the key functionalities I'm working on is obtaining the stock of blood by groups. I have successfully tested some JSON data using Postman, but the challenge now is in ...

utilizing $inject method along with supplementary constructor parameters

After referencing the answer found here: Upon implementing the $inject syntax, my controller code appears as follows: class MyCtrl { public static $inject: string[] = ['$scope']; constructor($scope){ // implementation } } // register ...