Resetting an object back to its initial value while preserving its bindings in AngularJS

My service deals with a complex object retrieved from an API, like this:

{
    name: "Foo",
    addr: {
        street: "123 Acacia Ave",
        zip: "10010"
    }
}

The initial value is stored in myService.address, and another variable holds a copy of it using

myService.originalAddr = angular.copy(myService.addr);

This setup allows me to reset a form to its original state when needed.

In my directive's controller, I bind a scope property to myService.addr by injecting the service:

$scope.addr = myService.addr;

I believe this establishes two-way binding.

When I execute a function in another directive:

$scope.reset = function(){
    myService.addr = angular.copy(myService.originalAddr);
}

The expected result is achieved as myService.addr resets to its original value. However, the bound values in my form do not reset, and $scope.addr retains the edited values. I have tried using

<input type="text" ng-bind="addr.street"/>

and also attempted

<input type="text" ng-model="addr.street"/>

Unfortunately, the form values remain unchanged after editing and are not reset. It appears that angular.copy is breaking the binding. Can you please advise on what I might be doing wrong?

Answer №1

Here is the recommended solution for resolving your issue:

$scope.reset = function(){
    $scope.addr = angular.copy(myService.originalAddr);
}

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

Splitting JavaScript Arrays: Exploring Efficient Division

I'm attempting to develop a recursive function that divides an array in half until it only consists of lengths of 3 and 2. After dividing, I want to neatly organize all the new arrays into another array. My idea is to find a way to determine the numb ...

Using Jest: A guide to utilizing a mocked class instance

When working on my frontend React application, I decided to use the auth0-js library for authentication purposes. This library provides the WebAuth class which I utilize in my code by creating an instance like so: import { WebAuth } from 'auth0-js&ap ...

Conflicts in SwiperJS Timeline Management

Having a Timeline on my Website using SwiperJS presents challenges with conflicting functions. The goal is to navigate swiper-slides by clicking on timespans in the timeline. The desired functionality for the timeline includes: Sliding to the correspondi ...

The final thumbnail fails to appear in the visible display (react-responsive-carousel)

I am currently facing an issue with displaying a series of images using react-responsive-carousel. When the images exceed a certain size causing the thumbnail section to become scrollable, the last thumbnail is always out of view. Although I have impleme ...

Stop Code Execution || Lock Screen

Is there a way to address the "challenge" I'm facing? I'm an avid gamer who enjoys customizing my game using JavaScript/jQuery with Greasemonkey/Firefox. There are numerous scripts that alter the DOM and input values. In my custom script, I hav ...

Canvg | Is there a way to customize canvas elements while converting from SVG?

Recently, I encountered an issue with styling SVG graphics dynamically generated from data. The SVG graphic appears like this: To address the problem, I turned to Canvg in hopes of converting the SVG into an image or PDF using a hidden canvas element. How ...

How to handle blank property values in JavaScript objects and convert them to null in an ASP.NET Web API

Hey there! I'm facing an issue where when I post a JavaScript object to an ASP.NET Web API, some property values are blank like the example below: var o={ ID=1, Fname="Tom", Mname="", Lname="Wilson" } However, in the Web ...

Issue with getStaticProps functionality on Vercel seems to be persisting

UPDATE: I realize now that my mistake was calling my own API instead of querying the MongoDB database directly in getStaticProps(). I have made the necessary changes: export async function getStaticProps() { await dbConnect(); const user = await User.find ...

React - the constructor binding issue with 'this' keyword

I am a beginner in React and I am learning through creating a simple test application. However, I am facing an issue with "this" binding. I set up this app package yesterday using "create-react-app", so all the necessary plugins including babel should be u ...

How to achieve a seamless iframe effect using CSS

With the introduction of new HTML5 iframe attributes, there are now more options available. One such attribute is the seamless attribute, which has the ability to: Create an iframe that appears to be seamlessly integrated into the containing document. ...

Tips for submitting a request following a change in the variable

I am in the process of developing a React application and I have implemented Auth0 for authentication. My goal is to initiate an HTTP request upon page refresh, but only if the variable isLoading is false. This way, I can access the user object once the ...

creating a live updating dropdown menu using Node.js and MySQL

Upon a user clicking and selecting a client, I would like to dynamically update the region dropdown menu with options that are related to that specific client from the databaseenter code here This is my client table: Here is the Region table, where clien ...

Is there a way to alter the text color using JavaScript on the client side?

Is there a way to change the color of a list item in a ul based on whether it is a palindrome or not? Here is the JavaScript file I am working with: function isPalindrome(text){ return text == text.split('').reverse().join(''); } c ...

React Redux components are failing to render after changes are made to the state array

I'm fairly new to using React, Redux, and Thunk in my project. Right now, I'm facing an issue where I fetch an array from an API call in my action/reducer, but it doesn't re-render in the connected component/container that's hooked up t ...

Updating the view manually is necessary when using AngularJS $routeProvider and resolve feature

I am facing issues with $routeProvider in updating the view automatically. Currently, I am working on converting a chapter 22 example from Adam Freeman's Pro Angular book, originally based on Deployd, to Express-Mongo. Strangely, the automatic refres ...

The crash during compilation is triggered by the presence of react-table/react-table.css in the code

My code and tests are functioning properly, but I am facing a challenge with my react-table file. The react-table.js API specifies that in order to use their CSS file, I need to include import "react-table/react-table.css"; in my react-table.js file. Howev ...

"Unspecified error in Angular" encountered when implementing Mobile Angular UI in conjunction with Angularfire

I am currently working on developing a mobile app using Mobile Angular UI and integrating it with a Firebase database. However, I keep encountering an error when trying to include Firebase: Uncaught ReferenceError: angular is not defined at angularfire.m ...

Accessing and playing audio files from Amazon S3 within Python code

I am attempting to directly read an audio file from S3 using Python. Initially, I record the audio with the following blob settings: blob = new Blob(audioChunks,{type: 'audio/wav'}); Then, I upload this file to S3 using Django: req=request.POST ...

Guide on utilizing the h function in Vue3 for seamless binding and passing of properties and events from parent to child components

Utilizing Vue3 and naive ui for front-end development has been a challenge for me as I primarily focus on back-end development and lack expertise in front-end technologies. To enhance user interaction, I incorporated naive ui’s BasicTable along with an ...

Removing an article from a Vue.js localStorage array using its index position

I am facing an issue while trying to remove an item from localStorage. I have created a table to store all the added items, and I would like to delete a specific article either by its index or ideally by its unique id. Your assistance is greatly apprecia ...