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

Sending JSON data from AngularJS to PHP can be achieved by using HTTP POST requests or

I have exhausted all options. I am having trouble reading the JSON object in PHP. Am I passing the JSON data correctly to PHP? I have tried using $_POST, $_REQUEST and json_decode to retrieve the data but nothing seems to work. How can I successfully retri ...

Looking for a smart way to extract all the selected elements from a form?

I am attempting to retrieve all the checked items from this form using JavaScript. I have looked at previous solutions, but none of them fit my requirements. <form id="checkform" class="container" style="margin-top:20px;"> <input type="checkb ...

The ng-bind directive is functional when used with a textarea element, but it does not work with

Recently diving into the world of angularjs, I stumbled upon ng-bind and its interesting functionality when used as a directive for textarea: <input type="text" ng-model="review.start"/> <textarea ng-bind="review.start"> </textarea> I ...

Exploring discrepancies between two tables with the power of Javascript

const firstTable = document.getElementById('table_1') const secondTable = document.getElementById('table_2') const rows1 = firstTable.rows const rows2 = secondTable.rows for (let i = 0; i < rows1.length; i++) { for (let x in rows ...

What is the process for setting up basic http authorization using angular.js?

My backend setup follows a structure similar to what is explained in this article. I am looking to make a GET request using angular.js, just like curl does it, with authorization via HTTP headers: $ curl -u miguel:python -i -X GET http://127.0.0.1:5000/a ...

Is it advisable to authenticate/authorize static files employed in pages that require authentication?

I am in the process of developing a cutting-edge angular 2 application. All back-end functionality is handled through REST API, ensuring seamless communication between the client and server. For enhanced security measures, all APIs that require authentica ...

Implement a feature in JS/HTML where clicking on a button shifts a specific section of a row from one table to another while simultaneously deleting the remaining

I am facing an issue with two tables - addFriends and existingFriends. The addFriends table contains a button in the fourth column, which, upon clicking, should delete the corresponding row from that table and add it to the existingFriends table. Currentl ...

Angular Pagination: A Detailed Guide

My goal is to paginate 7 results using a custom pagination tag I created: <pagination ng-model="currentPage" total-items="array.length" max-size="maxSize" boundary-links="true"> </paginatio ...

A comprehensive guide on how to find keywords within an array and then proceed to make all text within the parent div tag stand

I am currently looking for a webpage that displays a list of products based on keywords from an array. Once it detects any word in the array, it highlights it with a red background - everything is working smoothly so far. However, I now wish for the script ...

Do you think it's important to include a maxlength validation message for an input field in Angular?

Utilizing the maxlength attribute on a form field not only enables Angular's default validation, it also restricts input beyond the specified length from being typed into the text box. So, does this imply that displaying an error message for violating ...

Is it possible to conceal the contents of a details tag without using a summary tag?

I'm looking for a way to hide the details tag without the summary. In my code, the summary is only visible when a condition [isvisible == false] is met. However, even when the summary is not visible, the details keyword is still shown and I want to hi ...

Creating a dropdown button in HTML table cells with JavaScript: A comprehensive guide

I'm attempting to create a drop-down button for two specific columns in my HTML table, but all the rows are being converted into drop-down buttons. I only want the "categorycode" and "categoryname" columns to be drop-down. $(document).ready(functio ...

What strategies can be utilized to raise the max-height from the bottom to the top?

I am facing the following coding challenge: <div id = "parent"> <div id = "button"></div> </div> There is also a dynamically generated <div id="list"></div> I have successfully implem ...

I am curious as to why the Bootstrap 4 dropright selected menu appears as a default browser button in Chrome, while it functions seamlessly in Firefox

An issue has been identified that is specific to Chrome. The dropright selected menu appears differently in Chrome compared to Firefox, where it displays correctly with a white background. !https://i.sstatic.net/BulRA.jpg The Bootstrap version being used ...

Is it possible to track the movement of two phones within a specific location?

I'm currently working on a mobile app using Ionic that requires the backend or another user to be able to detect the movement of a different user. Here's how it would work: I need to mark a location with specific coordinates and save them. Onc ...

Ways to change the CSS styles of components within App

When my app is in full screen mode, I need to increase the font size for certain components. Within my App.jsx file, I have a variable that adds the "fullscreen" class to the root DIV of the app when triggered. Instead of using a blanket approach like * ...

Embedding the scrollbar within the div container

I'm having trouble placing my vertical scrollbar inside my div and adjusting its position. To streamline the code, I have extracted a portion below: .toprightcontrols { margin: 0 3% 0 0; display: flex; position: absolute; justif ...

Issue with displaying website monitors

I am facing an issue with my website where it displays differently on various monitors. Below is the HTML and CSS code snippets: HTML <body> <div id="Links"> <a href="About.html"><img src="Slideshow/About.png" width="140em" />< ...

Updating view in AngularJS using Promises

I am currently facing an issue where I need to update the view of an Angular application from its controller using an ES6 Promise (fetch) resolved value. The challenge lies in the fact that Angular does not automatically update the view when my promise res ...

Error with Display of ASCII Character 62 in Superfish Menu

Currently, I have implemented the Superfish menu on my website. In order to indicate sub-menus within my menus, I have opted to utilize the character entity code &#62;, which historically has represented ">". Oddly enough, while viewing my website ...