Modifying a variable in $rootScope data will also update other instances of $rootScope data

Since I need to use the data in multiple controllers, I have stored it in $rootScope.currentCache. When the page loads, the data typically looks like this:

{
    System: "Foo Bar",
    StartDateTime: "2014-11-27T12:35:00"
}

(please disregard any formatting issues as they are not relevant).

In my controller, I have the following code snippet:

app.controller("CurrentController", function ($rootScope, $scope) {
    console.log($rootScope.currentCache);
    var currentData = $rootScope.currentCache;
    //processDate() converts StartDateTime into a user-friendly format
    currentData = processDate(currentData);
    console.log($rootScope.currentCache);
});

The first console.log() displays the date as:

2014-11-27T12:35:00

However, the second console.log() shows the date as:

Nov 27th 2014 at 12:35pm

I find this confusing because the date processing is done on the separate currentData variable.

Other controllers require the date in its original format. So, my question is why is this happening and how can I prevent it?

Answer №1

It's important to note that both currentData and $rootScope.currentCache are simply references to the same real object

{    System: "Foo Bar", StartDateTime: "2014-11-27T12:35:00" }
. So, if you assign a new value to currentData like {pit : 'isgood'}, it only affects currentData. However, if you change a property of currentData like currentData.System = 'foo2bar', it will modify both variables. You can try this:

var currentDataDate = $rootScope.currentCache.StartDateTime;
currentDataDate = processDate(currentDataDate);

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

Transmit an array in a post request with Node.js using the application/x-www-form-urlencoded content type

After attempting to send a post request to an API with post parameters as an array, I encountered difficulties. Here is how it can be done using cURL: curl http://localhost:3000/check_amounts -d amounts[]=15 \ -d amounts[]=30 I then tried to ach ...

JavaScript to resize images before uploading without displaying a preview

I'm searching for a way to prevent the need to upload large or heavy image files. I believe utilizing the HTML5 FileAPI library is the best solution for this task. All necessary features have been implemented (upload, re-ordering, etc.), so now I ju ...

How to initialize data in Vue.js components

I'm working on a Map.vue component and I need to update its data in Vue.js. However, I'm struggling to figure out how to do this: <template> <div> <div class="google-map" :id="mapName"> </div> </div> < ...

Items seem to vanish into thin air and become immovable when attempting to relocate them

I am attempting to create a unique grid layout with 3x3 dimensions, where each grid item represents a fragment of a single image. These items should have the capability to be dragged and dropped inside another 3x3 grid, in any desired sequence. I have hit ...

Transforming a JSON object into a list in C#

After exploring similar posts without success, I am reaching out here for help. I have a Json data stored in a hidden field that I am trying to access in the code behind file of my markup page. My goal is to convert this Json into a List and bind it to a ...

Transforming the output of a PHP script from an HTML table into an ion-list format tailored for the Ionic framework

Currently I am working on an application built with the Ionic framework. I have developed a PHP file to retrieve 'Users' data from my database and it is currently being displayed in an HTML table format. In the services section of the framework, ...

Encountering a Typescript error when attempting to access the 'submitter' property on type 'Event' in order to retrieve a value in a |REACT| application

I am facing an issue with my React form that contains two submit buttons which need to hit different endpoints using Axios. When I attempt to retrieve the value of the form submitter (to determine which endpoint to target), I encounter an error while work ...

Step-by-step guide on integrating the Tawk chat widget script into a Next.js React application

I am currently working on integrating a chat widget from Tawk into a next.js react app. In my _app.js file, I have added the script import tag and attempted to set up the widget like this: import Script from 'next/script' {/* <!--Start ...

I keep encountering an error in the where clause when trying to update MySQL. What could be

I encountered an issue stating Unknown column 'CR0001' in 'where clause' while executing my code. Strangely, the error seems to be related to the id_scooter column rather than CR0001. Below is the snippet of my code: var update = "UPDA ...

Analyze arrays within object properties to identify discrepancies between them

Stuck on a Programming Challenge I am currently working on a project that involves using a JSON file along with express/nodejs, and I have hit a roadblock with a specific task that requires the following steps: Using a post route, the aim is to identify ...

The Transition Division Is Malfunctioning

I've encountered an issue where I am trying to move a div by adjusting its left property, but no transition is taking place. Regardless of the changes I make to my code, the result remains the same. Below is the current state of the code. Can anyone i ...

Utilizing the onFocus event in Material UI for a select input: A comprehensive guide

Having trouble adding an onFocus event with a select input in Material UI? When I try to add it, an error occurs. The select input field does not focus properly when using the onFocus event, although it works fine with other types of input. Check out this ...

What might be the reason for my react-bootstrap modal not applying any styling?

I have two projects, one created by following a tutorial series and the other created independently later on, aiming to replicate the first project. I have narrowed down my issue to a basic comparison of index.js in both projects. This code is an edited v ...

How can I send my response as JSON in the controller in Asp.net?

Whenever I use toastr for form notifications, I always seem to receive an error message even though the data is successfully saved in the database. I suspect that the error may be stemming from the controller because I am not sending back in JSON format. A ...

The slicing of jQuery parent elements

Hey there! I recently created a simulated "Load More" feature for certain elements within divs. However, I've encountered an issue where clicking on the Load More button causes all elements in both my first and second containers to load simultaneously ...

Is a finished callback needed for .on('xxx') event?

On my dashboard page, I am currently retrieving the top 25 comments and displaying them using the following code: fba.orderByChild('when').limitToLast(25).on('child_added', function (d, c) { stuff }); However, the function is called f ...

Implementing an Asynchronous Limited Queue in JavaScript/TypeScript with async/await

Trying to grasp the concept of async/await, I am faced with the following code snippet: class AsyncQueue<T> { queue = Array<T>() maxSize = 1 async enqueue(x: T) { if (this.queue.length > this.maxSize) { // B ...

Ways to pass scope between the same controller multiple times

There is a unique scenario in which I have a controller appearing in 2 different locations on a page. This arrangement is necessary for specific reasons. To illustrate, the simplified HTML structure is as follows: <aside ng-if="aside.on" ng-controller ...

What is preventing me from generating a new User account?

I'm currently working on setting up a register and login server using node.js and mongoose. I have created a user model and a user route, but for some reason, I am unable to successfully create a new user. When I try to connect to Postman using POST : ...

Tips for developing a dynamic game that adjusts to different screen sizes using Phaser 3

Recently, I've been on the hunt for a way to ensure my game adapts seamlessly to various screen resolutions when using Phaser 3. In the past, I achieved this with ease in Construct 2. However, I'm now curious to explore how best to implement thi ...