Making changes to an AngularJS property updates the value of an HTML attribute

One of my pages, base.html, contains the following code:

<html lang="en" data-ng-app="MyApp" data-ng-controller="MyController">
<body style="background-color: [[ BackgroundPrimaryColor ]]">
.
.
.
{{ block content }}
{{ endblock }}
.
.
.
</body>
<script>
// Creating an AngularJS Module for our AngularJS Application
var app=angular.module("MyApp",[]);

// Changing syntax for AngularJS Expression
app.config(function($interpolateProvider) {
  $interpolateProvider.startSymbol('[[');
  $interpolateProvider.endSymbol(']]');
});

// Defining AngularJS Controller for our AngularJS Application
app.controller("MyController",function($scope){
    // Defining AngularJS Application Property to specify Mode of the Website
    $scope.DarkMode=false;

    // Defining function to change Mode of the Website
    $scope.changeDarkLightMode=function(){
        // Changing other values
        if ($scope.DarkMode){
            $scope.BackgroundPrimaryColor="black";
        }
        else{
            $scope.BackgroundPrimaryColor="white";
        }

        // Changing value of the property DarkMode
        $scope.DarkMode=!$scope.DarkMode;

    };
    
    // Calling changeDarkLightMode() whenever page loads
    $scope.changeDarkLightMode();

});
</script>
</html>

Another page in my project is called contact me.html and it includes the following code:

{{ block content }}
<body onload="onLoad()">
.
.
.
</body>
<script>
function onLoad(){
 document.getElementsbyTagName("body")[0].setAttribute("style","background-repeat: no-repeat;background-attachment: scroll;background-image: url({% static 'Contact_Me_Image.png' %});background-size:cover;background-position: 0% 20%;");}
</script>
{{ endblock }}

Essentially, the contact me.html page is a part of the base.html.

In the contact me.html page, I am dynamically setting the styling of the body tag using JavaScript. However, when I change the value of the AngularJS Property BackgroundPrimaryColor, the style changes back to its initial state.

If anyone knows why this happens, please let me know.

Answer №1

please refrain from using custom "[[" interpolation, instead favor the use of "{{" default tag

<body style="background-color: {{BackgroundPrimaryColor}}">
...
</body>

and in your controller, implement the following:

app.controller("MyController",function($scope){

$scope.BackgroundPrimaryColor="#FFF";

$scope.init=function(){
    $scope.BackgroundPrimaryColor="red";
}

$scope.init();

});

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

Is there an easy method to verify if the local storage is devoid of any data?

Query is named aptly; seeking to verify in a conditional statement. ...

Unable to connect to the cloud firestore backend specifically on the deployed version

When deploying the project using Vercel, I included my Firebase security and project details as environment variables on the Vercel dashboard. Authentication works fine during deployment, but there is an error in the console: @firebase/firestore: Firesto ...

Horizontal alignment of Bootstrap columns is not working as expected

While attempting to integrate Bootstrap elements into my portfolio, I encountered issues with aligning columns. Currently, the only columns I have added are in the "welcome-section" (Lines 40-52), but they refuse to align horizontally. I've tried tro ...

Several jQuery ajax requests seem to be failing due to errors in previous calls

I am experiencing an issue with my website's login form that utilizes ajax to send the login information to php. After successfully logging in, users have the ability to log out and then return to the login page again. However, I am facing a problem w ...

Ways to specify distinct css styles according to varying number of elements

I need the Button element to always take up 100% of the container, no matter how many elements are inside. Check out my Fiddle link here: Js Fiddle Link <div class="inputFieldBoolean buttonSeries" data-type="Boolean"><button class="true" data-va ...

Utilize the useRef hook in React to enable copying text to the clipboard

Looking to implement a copy to clipboard functionality in React using the useRef hook? Want to accomplish this without relying on any additional libraries? Take a look at my code snippet below. Currently, I'm encountering an error stating myRef.curren ...

Tips for making the background image fit perfectly within a div element

I have a calendar div that I want to customize with a background image and text. How can I achieve this? Here is my code: .calenderArrivalDiv{ margin-top: 15%; margin-bottom: 1%; background-image: url("im ...

JavaScript - the global and local variable dilemma

REVISED2: I'm encountering an issue with converting images to canvas using Pixastic in HTML5. How can I 'return' this converted image back to a global variable? Any suggestions? <img id="mainIllustration" alt="main illustration" src="Img ...

Utilizing an Immediate-Invoked Function Expression (IIFE) for jQuery in JavaScript

I'm looking at this code snippet that I believe is an Immediately Invoked Function Expression (IIFE). But, I'm confused about the role of (jQuery) and ($). I understand that it involves passing a reference to jQuery into the IIFE, but can someone ...

Accessing a parent class constructor object in NodeJS from the Child Class

I'm currently working on creating a Controller Class that will handle the initialization of all my routes using ExpressJS. Below is a simple example of what I have so far: class Test extends Controller { constructor(App) { const Routes = [ ...

Leveraging Angular's catchError method to handle errors and return

One of my challenges involves a model class that represents the server response: class ServerResponse { code: number; response: string; } Whenever I make api calls, I want the response to always be of type Observable<ServerResponse>, even in ...

React: The row flex-direction is not being applied as expected

Although I know there are countless identical questions out there, mine lacks context and is as generic as they come. Why isn't the flex-direction row property working? React View return ( <Fragment> <h1>The About us page.</ ...

Passing a variable from JavaScript to PHP, utilizing the variable in an SQL query, and showcasing the results through PHP

I need to pass a JavaScript variable to a PHP script, incorporate this variable into my SQL query, and then return the results back to JavaScript. JavaScript: // Sending a variable to PHP script var variableToSend = '30'; $.post('myPHPscri ...

Data retrieval error, function returned instead of expected value

Hey everyone, I'm currently working on fetching data using the GET method and I would like the data to be displayed after clicking a button, following standard CRUD operations. As a beginner in programming, I could use some help. Any assistance is gre ...

Create Stylish Popups and Spacious Containers with CSS

A unique pure css popup was crafted by merging and . Hovering over "John Smith" triggers the appearance of the popup. The code for this can be seen at http://codepen.io/kikibres/pen/MwEgQX. However, there are some challenges being faced. The aim is to ke ...

Programming the tab pages within Chrome

By using JavaScript, I successfully opened a new tab at www.blogger.com from the Main.html page. <script> window.open("https://www.blogger.com"); </script> I am now on the blogger page. My goal is to automatically scroll to the end of the bl ...

Is there a way for me to connect the ajax data to Vue components?

Utilizing Jquery ajax to load data from an external API has been successful and the v-for text is also working without any issues. Vue var vm = new Vue({ el:'.inlinePoetry', data:{ PoetryList:[] }, created:function(){ var ...

How to trigger an event with multiple parameters up several levels using Vue 3?

Important Note: While this question may seem repetitive to some, I have not been able to find a suitable solution for Vue 3 that involves passing events with parameters through multiple levels. Please correct me if I am mistaken. A Clarification: Despite ...

Retrieve information from a text

I retrieved this data as a string from a webpage using jQuery and need help parsing it. When I attempted to use jQuery.parseJSON, I encountered the error Uncaught SyntaxError: Unexpected token n. The specific values I am looking for are "surl" and "imgur ...

Enzyme in Action: Using React.js to Emulate a Click Event

I have been working on a React.js application which is a straightforward Cart app. You can take a look at the code sandbox here: https://codesandbox.io/s/znvk4p70xl The issue I'm facing is with unit testing the state of the application using Jest and ...