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

Utilizing React Hook Form for Interactive Slider and User Input Controls

Whenever I move the Slider, I want to retrieve the same value as when I enter a new value in the Input field. However, the current setup is not functioning correctly. <StyledSlider {register(NAME ...

What is the most effective way to ensure that a child component only executes when a link is clicked on the Vue component?

There are two components in my code The first component, which is the parent component, looks like this : <template> <ul class="list-group"> <li v-for="item in invoices" class="list-group-item"> <div class="ro ...

Optimal approach for verifying the POST request payload

My RESTful API has a POST endpoint for user creation, and I am looking to implement data validation before inserting it into the database. There are two approaches I'm considering: Approach 1: Incorporating model validation in the controller and repe ...

retrieve the value of an HTML element once it has been modified

When I am working in a view, I encounter an issue where I need to retrieve the value of an HTML input box after it has been changed. Initially, the page loads with the following code: <input id="input_one" type="text" value = "apple" /> Upon loadin ...

Problem with animated scrolling in Jquery

Currently, I am working on a function that aims to smoothly scroll the web to a specific div each time a user scrolls "into" a container div. You can get a better idea of what I mean by looking at my JSFiddle. Everything works perfectly when the user scro ...

How to display text on a new line using HTML and CSS?

How can I properly display formatted text in HTML with a text string from my database? The text should be contained within a <div class="col-xs-12"> and nested inside the div, there should be a <pre> tag. When the text size exceeds the width o ...

Arrangement of jQuery On Events and Triggers

Consider the code snippet below: $('body').on('hellothere', function(){ console.log('execution complete.'); }); $('body').triggerHandler('hellothere'); The abov ...

AJAX allows developers to declare variables within their code to

Here is a snippet of my JavaScript code: $(\"img.agree\").click(function() { var follow_id = $(this).attr(\"id\"); var user_id = $(\"img.user_id\").attr(\"id\"); $.ajax({ type: \"POST& ...

"Auth.currentSession is indicating that there is no user currently logged in

I am currently working on a basic React app with authentication using aws-amplify. My user pool is set up in Cognito and I can successfully redirect the user to the hosted UI for login. However, when trying to retrieve the current session, I am receiving a ...

Received undefined value when trying to access a variable in AngularJS

$http({ method: 'GET', withCredentials: true, url: 'http://...' + $scope.orderId }).success(function(data) { $scope.order = data; $scope.products = $scope.order.items; * * $scope.shopid = $scope.order.shop_id; * * $scope.pac ...

Fetching Information From Database Using PHP

I am encountering an issue while attempting to transfer data from my HTML table to my modal (without using bootstrap). In the image below, you can see that I have successfully displayed data from MySQL database in the table. The problem arises when I clic ...

Having trouble with the clear button for text input in Javascript when using Bootstrap and adding custom CSS. Any suggestions on how to fix

My code was working perfectly until I decided to add some CSS to it. You can view the code snippet by clicking on this link (I couldn't include it here due to issues with the code editor): View Gist code snippet here The code is based on Bootstrap. ...

Having trouble with a jquery link not working even after removing the 'disabled' class

I am currently using a script that disables links with the class "disabled" //disable links $(document).ready(function() { $(".disabled a").click(function() { return false; }); }); In addition, I have another script that toggles the disabled s ...

Discovering elements that are currently visible in an Angular list

Suppose there is a variable named users which represents an array We have the following ng-repeat statement: ng-repeat="user in users | filterUser: searchUser: assignedUsers: selectedDivision" After filtering the users using searchUser and selectedDivis ...

How can we reduce the size of a JSON object in Typescript before sending it to the client?

Currently, I am faced with a common challenge. I have a database object that is a standard JS object originating from a document database, and my goal is to transmit this object to the client. However, certain fields contain sensitive information that shou ...

Creating a new object in an empty array within my profile model (mongodb/mongoose) is simple. Just follow these steps to successfully add a

Presenting my Profile model: const ProfileSchema = new mongoose.Schema({ user: { type: mongoose.Schema.Types.ObjectId, ref: "User", }, company: String, website: String, location: String, status: { type: String, require ...

Is there a way to effectively incorporate react-native with php and ensure that the data returned by the fetch function is

I'm struggling to make my return value work in this code. Has anyone had success using react-native with php and fetch json? Any tips or advice would be greatly appreciated. PHP $myArray = array(); $myArray['lat'] = $_POST['lat']; ...

Having difficulty adjusting the StartIcon margin within the MUI Button example, unable to override its values

Currently, I am facing an issue with the Button component in Material UI. Specifically, I am working with a Button that contains a StartIcon. Despite trying to modify the default margin provided by the StartIcon CSS, I have been unsuccessful so far. https: ...

The function call to 'import firebase.firestore()' results in a value

I recently set up a Vue App with the Vuefire plugin. Here is an example of my main.js file, following the documentation provided at: : import Vue from 'vue' import App from './App.vue' import router from './router' import sto ...

Adjust the CSS styling to ensure that the div or iframe expands to fit the entire height of the

Having trouble making a map embedded via an iframe on a WordPress page display responsively at full height independent of the device? Check out: . To clarify, by full height, I mean that the bottom of the map should not extend beyond the screen, eliminati ...