Update the ng-model in AngularJS when the value is set to true

Hello there, I am in the process of developing an app that provides a summary of data from a database. In this app, there is a form that populates input fields with information using ng-model. Users can edit these values as needed.

However, I want to ensure that the summary does not update automatically when users make changes to the input fields. I only want the changes to be reflected once the form has been submitted.

On the overview side, here is the HTML code:

<dt>COMPANY</dt>
<dd>{{client.company}}</dd>
<dt>NAME</dt>
<dd>{{client.firstname}} {{client.lastname}}</dd>

And here is the form code:

<p>Company</p><input type="text" name="ciCompany" class="pinfo" ng-model-options="{ updateOn: 'value = true' }" required ng-model="client[0].company">
<p>First Name</p><input type="text" name="ciFirstName" class="pinfo" ng-model-options="{ updateOn: 'value = true' }" required ng-model="client[0].firstname">
<p>Last Name</p><input type="text" name="ciLastName" class="pinfo" ng-model-options="{ updateOn: 'value = true' }" required ng-model="client[0].lastname">

I'm uncertain whether using ng-model-options is the correct approach. However, this setup aligns with the intended functionality. Any guidance or suggestions would be appreciated.

Thank you!

Answer №1

In order to achieve this, you have the option to utilize angular.copy:

$scope.clients = {}; // the array
$scope.clientsCopy = angular.copy($scope.clients);

<dt>COMPANY</dt>
<dd>{{clientsCopy.company}}</dd>
<dt>NAME</dt>
<dd>{{clientsCopy.firstname}} {{clientsCopy.lastname}}</dd>

<form ng-submit="userSubmit()">
  <p>Company</p><input type="text" required ng-model="clients[0].company">
</form>

Afterwards, you can make use of the onSubmit function to update the new values:

$scope.userSubmit = function() {
  $scope.clientsCopy = $scope.clients; 
}

If you prefer, you also have the choice to employ _.extends or _.merge when working with underscore or lodash.

Answer №2

When submitting a form, it is important to remember to use the angular.copy method in order to duplicate your data properly, as shown below:

$scope.user = angular.copy(users[0]);

For more information, you can check out this helpful resource

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

Can several identical directives access and manipulate the same scope variable?

Is the scope of a controller shared when using the directive multiple times on one page? I'm not sure, but I would appreciate some help understanding this. If I define the variable 'isWidgetEnabled' in fooController, will it be shared betwe ...

Performing an AJAX call using jQuery within a PhoneGap application to communicate with a Node JS server

I've searched everywhere online for a practical demonstration of a jQuery AJAX call to a Node JS server, but to no avail. Typically, a jQuery AJAX request to a PHP server follows this format: $("button").click(function() { $.ajax({url: "http://w ...

Rendering a Vue select list before receiving data from a Meteor callback

I am currently facing an issue with populating my events array from a meteor call so that it appears in a select list. The 'get.upcoming' Meteor function returns an array of JSON objects, but it seems like the select list is being rendered before ...

Returning a 404 Error stating "Invalid request to /api/users/register."

Encountering an issue with proxy connection - unable to determine the root cause despite verifying all routes. Not able to successfully register the user and store data in MongoDB. Seeking suggestions for resolution. Thank you. Attempting to send user reg ...

Encountering a problem with parsing a JSON object using the .map

After receiving this JSON response, my main goal is to extract the value located in the identifier. By utilizing console.log, I am able to analyze the structure of the object: Object {rows: Array[33], time: 0.015, fields: Object, total_rows: 33} fields: O ...

Displaying svg files conditionally in a react native application

I have developed an app specifically for trading dogs. Each dog breed in my app is associated with its own unique svg file, which are all stored in the assets folder (approximately 150 svg files in total). When retrieving post data from the backend, I re ...

Make sure the inputs in separate table data cells are lined up in

I need help aligning two input fields in separate td elements to be on the same line. The issue I am encountering is that when an input is added to a td, it covers up any text within the td. https://i.stack.imgur.com/c7GiQ.png There are two scenarios: I ...

Encountered an error 'Unexpected token ;' while attempting to include a PHP variable in a jQuery AJAX call

Trying to execute a PHP script that updates a deleted field in the database when you drag a specific text element to a droppable area. Currently, here is the droppable area code: <div class="dropbin" id="dropbin" > <span class="fa fa-trash-o ...

Traversing a two-dimensional array backwards in JavaScript

I am working with an array that contains different teams: The structure looks like this: leagues = new Array( Array('Juventus'), Array('Milan'), Array('Inter')); My goal is to iterate through the array and generat ...

Is there an official IRC channel for YUI?

Although the yui3 documentation is quite helpful, it can also be beneficial to ask unconventional questions in order to discover the best practices. Are there any gatherings or meetups for all the talented yui developers out there? ...

Having trouble importing images in React and passing them as a prop?

I'm attempting to import images, place them into an array, and then pass that array to a prop in a component to display different images. However, after passing the array to the component, the items accessed from the array are showing as undefined, pr ...

Is it possible to load a JS file without using the require function?

Is there a method to load a JavaScript file without using require, but with fs for instance? I am aware that for JSON files I can utilize: const jsonFile = JSON.parse(fs.readFileSync("/jsonfile.json")) Can the same be done for a JavaScript file? I am inq ...

Tips for rearranging table columns using drag and drop in react js

I have been experimenting with various libraries to create a drag-and-drop feature for changing table columns order. Here is my current implementation: import React, { useState } from 'react'; import './list_de_tournees.css' const Table ...

The use of async components in Vue.js with named imports

I understand how to dynamically load components asynchronously in Vue. For example, instead of importing MyComponent directly: import MyComponent from '@/components/MyComponent' export default { components: { MyComponent } } We can use ...

Populate a dropdown list with array elements using Javascript in ReactJS

I am encountering an issue while trying to populate a dropdown with data from the 'options' array. The error message states that it cannot read property appendChild of null. Using 'document.getElementByClassName' instead of document.ge ...

What is the best way to show the contents of an array after making a getjson request?

My function makes two getJSON calls and writes the responses to an array. At the end of the second getJSON call, I used the code snippet below: alert(files.length); print_r(files); console.log(files); However, the alert shows the correct number of items ...

Oops! Dropzone encountered an error because no URL was provided

I am currently working on a form that includes both HTML and JavaScript code. The form looks like this: <form class="block-center" id="pdfForm" method="POST" action="form_threatment.php" enctype="multipart/form-data" style="margin-top: 30px;"> ...

When working with JavaScript and Node.js, it is not possible to access an object's index that is

Utilizing babyparse (PapaParse) in nodejs to convert CSV to JavaScript objects has been quite challenging for me. After processing, the output of one object looks like this: { 'ProductName': 'Nike t-shirt', ProductPrice: '14.9 ...

"Trouble with Express.js: CSS isn't loading, yet HTML displays

Click here to view folders and files server.js is the code I have created so far //importing packages const express = require('express'); const admin = require('firebase-admin'); const bcrypt = require('bcrypt'); const path = ...

Ways to pass data to a different module component by utilizing BehaviourSubject

In multiple projects, I have used a particular approach to pass data from one component to another. However, in my current project, I am facing an issue with passing data from a parent component (in AppModule) to a sidebar component (in CoreModule) upon dr ...