Sharing a copied object between AngularJS controllers

I'm facing an issue in my AngularJS project where I can't share copied object data between different controllers.

While I can successfully share data by setting properties in code, copying the object seems to cause a problem.

In the provided code snippet on JSFiddle, you'll find two objects named User and OriginalUser. Upon loading the FirstCtrl, I copy the contents of User into OriginalUser.

Although the value assigned to User object in FirstCtrl is accessible in SecondCtrl, the data within OriginalUser is only available in FirstCtrl.

To see this behavior firsthand, visit the live demo on JSFiddle Code Sample

Answer №1

The reason for this issue is that instead of updating the OriginalUser data, you are creating a new variable using the angular.copy function.

What you actually need is to use angular.extend, which will copy the user properties to the original user object. This way, both controllers will be using the same object instance.

In your first controller:

function FirstCtrl($scope, User, OriginalUser){
    $scope.user = User;
    $scope.o_user = angular.extend(OriginalUser, $scope.user);
}

Your fiddle has been updated accordingly.

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

How about "Can I use JavaScript with wget?"

I recently developed a webpage that leverages client-side JavaScript to manipulate data on the page prior to being presented to the user. I am curious if there is a way to utilize the wget command to retrieve the page and apply a client-side JavaScript en ...

Ways to determine if the mouse is positioned at the bottom of a div area

I have multiple sections and I am trying to change the mouse cursor when it is within 200px of the bottom of each section. Although I used the code provided, it only seems to work for the first section. The e.pageY value is not being reset in subsequent s ...

Strategies for adjusting image components based on various screen sizes

Let's consider a scenario: There is an image nested inside an introductory div for the desktop layout, like so: <div class="intro"> <img id="gambar" src="assets/images/image-intro-desktop.jpg" alt=&q ...

Completing online form text entries in WebView without the need for identifying elements

I am currently working on a project to automate filling out an online login form using local string variables. This is the progress I have made so far: web = (WebView) findViewById(R.id.webview); WebSettings webSettings = web.getSettings() ...

I'm having an issue with my Bootstrap tabs - they seem to be unresponsive when clicked

I've been working on a Bootstrap website and have run into some issues that I can't seem to resolve. One problem I encountered was with a set of tabs that were supposed to be interactive, but for some reason, they weren't working as expected ...

Jquery results in an error and causes CSS elements to become stuck

Here is the code I am currently using on JSFIDDLE: JSFIDDLE The issue I am facing is that when an item is dragged to a correct box, it disables the original item from being dragged again. However, this causes the CSS to stick, preventing the green color o ...

Setting a value in the selectpicker of rsuitejs involves assigning a specific value to the

const _DATA = [{ code: 'code1', name: 'Jagger' },{ code: 'code2', name: 'Tigger' },{ code: 'code3', name: 'Lion' }] <SelectPicker data={_DATA.map(x => {return {label: x.n ...

What is the reason that when a variable is embedded within another variable, its value remains anchored to its initial declaration?

When looking at the code below, one would anticipate the output to read "text something different" but instead it displays "text something". var dynamic = "something"; var thistext = "text " + dynamic; dynamic = "something different"; console.log(this ...

Creating a React lightbox feature where users can click on the first image and view it, but the subsequent images are displayed as empty

While creating this lightbox in React, I encountered an issue where the next image in the array appears empty and shows "Object object" as the error. I am extracting both the image and text from the array of objects. Does anyone have any suggestions? The ...

Following the modification of the Context root, the JSP page is no longer functioning as expected

Recently, I developed a JSP page within the webapp that utilizes jquery, CSS, and Angular JS. The files jquery-1.12.0.js, angular.min.js, and TableCSSCode.css are all located in the same directory as the JSP page. Initially, my application context was set ...

Adding the output from a promise to an array

I am attempting to clear out and then refill an array with values retrieved from a promise. However, I've noticed that the values don't always get added back in the same order. $scope.$watch ('timeRange', function (newValue, oldValue, ...

Is it possible to update my services list based on which checkboxes I choose to uncheck at the top?

i am currently tackling a small project, and I have limited experience with javascript and json. Can someone assist me in resolving the final step? I am on the verge of completing it, but encountering some issues. My goal is to filter the results based on ...

What is the best way to transfer a boolean value from a cshtml file to a javascript function or constructor?

I'm encountering an issue where a boolean variable from the Model in my cshtml file is showing up as "undefined" when passed to the javascript constructor. How can I successfully pass this boolean value for use during the execution of the javascript c ...

"Utilize the addEventListener function in combination with the 'for' keyword to

I have been working on a Chrome extension where I use a "for" loop to change the display style of elements named "mode" to none, except for one which should be displayed at the end of the loop. However, whenever I click on the button to activate it, my tab ...

New HTML5 feature: using the input type duration in forms

How can I utilize input type duration? I want users to be able to input a time duration in the format of 06:30:27:15 ( hh:mm:ss:ms ), allowing for values between (0-23:0-59:0-59:0-59). Any assistance would be greatly appreciated! IMPORTANT!: This impleme ...

Incorporate various variables within a JavaScript loop

I have a range of input classes from .fat-calc1 to .fat-calc24, as well as a designated result area with the class fat-result. My goal is to create a loop that accumulates all inputs (.fat-calc1 + .fat-calc2 + ... + .fat-calc24) and presents the sum in the ...

Change the value of a particular property in an array of objects by utilizing an inline function

I am working on updating the price field for a specific object. Below is my selectedCurrenciesArray: const [selectedSwapCurrencies, setSelectedSwapCurrencies] = useState([ { symbol: null, logo: null, price: 0 }, { ...

Utilizing jQuery's .load() method to import a scripted section from one page into another

I have a container with dynamically generated content using jQuery on Page 1. Now, I want to transfer this container to Page 2: <div id="latestNews"> <h4>Latest News</h4> <ul> <!--Section automatically loaded here and to ...

VueJS can manipulate an inline template by dynamically changing its content and reinitializing

this particular query shares similarities with the discussions on VueJS re-compiling HTML in an inline-template component as well as How to implement Vue js directive in an appended html element Regrettably, the approach suggested in those threads is no l ...

What is the best way to utilize useRef on a component that is not accessible within React BigCalendar?

I'm currently working with React Big Calendar (https://github.com/intljusticemission/react-big-calendar) and facing a challenge with responsive styling. I need to detach the horizontal scrollbar (overflow-x) of a specific div, .rbc-agenda-view, and at ...