Enhancing AngularJS Variables

I have a global variable that I am updating with JSON data upon clicking. Strangely, the global variable gets updated even when I'm not actively clicking on anything.

$scope.save = function() { // method invoked when clicked
    DataBasket.users = $scope.usersPermissions; // DataBasket is actually a factory responsible for updating the global variable
}

On subsequent clicks, I manipulate the value of $scope.usersPermissions, but I don't want these changes to be reflected in the global variable.

How can I resolve this issue?

Answer №1

To achieve what you are looking for, ensure that DataBasket.users is a separate copy of $scope.usersPermissions. You can accomplish this by implementing the following:

$scope.save = function() {
    DataBasket.users = angular.copy($scope.usersPermissions);
}

If not, DataBasket.users will just point to the same data as $scope.usersPermissions, causing any changes made in one object to reflect in the other since they are essentially the same.

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

Make button text bold once clicked

Is there a way to change the text of a button to bold when it is clicked on? I've been looking for solutions and came across this question: How to make a text in font/weight style bold in JavaScript http://jsfiddle.net/14tjwvnq/ function boldButton( ...

Adding a command to open a new browser tab using JavaScript code can easily be accomplished by following these steps. By using Terminal, you can quickly and efficiently implement this feature

I'm new to coding and trying to create a terminal simulation. You can check out my code here: https://codepen.io/isdampe/pen/YpgOYr. Command: var coreCmds = { "clear": clear }; Answer (to clear the screen): function clear(argv, argc ...

Referring one sub-document to another sub-document in Mongoose

In my customer schema, there are two sub-document sets; orders and children, here is the structure: const mongoose = require('mongoose'); const Schema = mongoose.Schema; const ObjectId = Schema.ObjectId; const childrenSchema = new Schema({ &a ...

Tips for smoothly switching from one SVG image to another

I am looking for a way to smoothly transition between two SVG images that are set as background images in a div. I am specifically interested in using CSS 3 transitions for this effect, but I am open to other solutions as well. Can you help me achieve th ...

"Troubleshooting a jittery React sticky header during scrolling: Dealing with IntersectionObserver and CSS

My current project involves implementing a sticky header in a React application using the Carbon framework. The goal is to have a sticky header with a bottom border when users scroll up, providing context for their viewing experience. However, when they sc ...

Storing an image as an encoded string in MongoDB: Step-by-step guide

Currently, my goal is to transform an image into a string and store it in MongoDB. Additionally, I would like the ability to decode it at a later time. My approach involves solely using Express, MongoDB, and ReactJS. I specifically do not want to upload t ...

The connections between children in the Highcharts Org chart are getting tangled up

I am facing an issue with the organization chart I created using highcharts. Specifically, at the 3rd level, the links are becoming merged or overlapped with other child links. The data for the chart is being sourced from an ERP system and contains informa ...

What causes the disappearance of the cell background color upon clicking?

Click here to view my code snippet. You can access the demo at this link. Overview: I am currently working on creating a grid that allows users to select cells by dragging the mouse cursor over them. Additionally, the background color of each cell shoul ...

Is there a way to assign a variable as the value for the jQuery .attr method?

How can I use jQuery's attr() method to pass a variable as the value for the 'src' attribute? Here is an example of what I am trying to achieve: var under700 = 'image.jpg' $('.two_images img').attr('src', (und ...

How can I populate a form in Meteor with data from a MongoDB collection that was previously inserted?

Recently, I completed constructing a lengthy form field where users can enter a plethora of information. This form consists of various text and number fields, radio button sets, and checkbox groups. The data is successfully saved in a Mongo collection with ...

The perplexity of variables

When it comes to reading more elements P in a function, the question arises: is it more efficient to create pElem every time in a loop? dataStr * process(char *start, char *stop, GTree* tree) { while ( (cp != NULL) && ( cp < nextI)) { ...

Assign AngularJS directives to DOM element using controller

I'm experimenting with the idea of controlling the angular directive('ng-show') from the controller. Here is what I'm attempting to do: var node = `<div ng-show="this.length > 5">...</div>`; var child = document.creat ...

Tips for adjusting $(document).height() to exclude the height of hidden divs

I am facing an issue with jQuery miscalculating the space at the bottom of a page I'm working on, possibly due to hidden div layers present on the page. Is there a way for jQuery to accurately calculate the 'real' height of the page as it a ...

"Code snippets customized for React are not functioning properly in the javascriptreact.json file within Visual Studio Code

I'm looking to incorporate some customized React.js code snippets into my Visual Studio Code setup. I am aware of the two files in VSCode that handle this - javascript.json and javascriptreact.json. Although the snippets work well in javascript.json, ...

What is the best way to add a property and its value to objects within an array, especially those which do not currently have that specific property?

My goal is to: Iterate through the peopleData array, add a property named 'age' and assign it a value of '-' for any objects in the array that do not have the key 'age' const peopleData = [ { name: "Ann", age: 15, email: ...

Utilizing AngularJS to retrieve associated information from users via a JSON API

I'm currently utilizing the Ionic framework and my inquiry relates to AngularJS. I've set up a JSON API using Ruby on Rails and for authentication, I've opted for ng-token-auth + devise-token-auth. User JSON: { "data": { "id": "1", ...

Trouble with closing windows in the in-app browser on Android devices

Is there a method to successfully close the in-app browser? Despite window.close working on iOS devices, it is not effective on Android. I have experimented with alternatives like window.top.close and window.open("", "_self") window.close, but none have ...

Once the form has been submitted, proceed to open a new page following processing

Trying to remove an entry or offer from the database and then return to the page with all entries/offers. Attempted using Javascript, but it successfully deletes the item without redirecting to the overview page. Is this the correct approach with Javascri ...

Ensure the video frame stretches to fit the full width

I am struggling to make my video frame fill the entire width of the screen. Even though I have tried using CSS, the video container does not stretch to the full width as expected. https://i.sstatic.net/kxyE0.png How can I ensure that the video plays acro ...

merge a pair of scopes within Angular

I have a dilemma with merging two different scopes. Can someone suggest the most efficient method to achieve this? Here are details of the scopes in question; the second one originates from a service. The initial scope retrieves information from the datab ...