Trigger ng-model value update on input box by force in JavaScript

<form>
      Low Range: <input type="number" name="lowRange" 
            ng-model="weight" ng-model-options = "{updateOn:'submit'}">
      <button type="submit">Assign</button>
      <button type="button" ng-click="resetValue()">Discard</button>
</form>
<p>The value of <strong>weight</strong> is connected to <code>ng-model. Using the directive ng-model-options="{updateOn:'submit'}", the value of ng-model only updates upon clicking the submit button. 
If a user modifies the input value and clicks discard, the intention is to reset the input box to match the current ng-model value.
This is achieved by calling a function with ng-click="resetValue()"

$scope.resetValue = function(){
    $scope.weight = $scope.weight; // Adding +1 here would update ng-model but not needed
  }

However, when attempting to assign the same variable values, it doesn't work as expected due to reference assignments. The value in the input box remains unchanged. Is there a way to force the input value bound to ng-model to reflect the updated value?

Check out a live demo here: http://plnkr.co/edit/NQTieUmdEbxZeBsjk8Jw?%2F=preview&p=preview

Answer №1

To reset the input field, you must specify it explicitly.

<form name="myForm">
  Start: <input type="number" name="startNum" ng-model="value" ng-model-options="{updateOn:'submit'}">
  <br>
  <button type="submit">Set</button>
  <button type="button" ng-click="myForm.startNum.$rollbackViewValue();">Reset</button>
</form>

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

Using HTML, jQuery, and JSON with an AJAX call to populate two web tables stacked on top of each other

I am encountering an issue with populating two tables using two searches based on user input in mySQL-JSON-AJAX. When the user enters a search term and clicks the corresponding button, data should populate the respective table. The problem arises when clic ...

Error: The addDoc() function in FireBase was encountered with invalid data, as it included an unsupported field value of undefined during the execution

As I attempt to input data into the firebase database, an error arises: 'FireBaseError: Function addDoc() called with invalid data. Unsupported field value: undefined'. The registration form requests 2 inputs - name and email. The function handle ...

What is the process for retrieving my dates from local storage and displaying them without the time?

Having an event form, I collect information and store it in local storage without using an API. However, I face a challenge when extracting the startdate and enddate out of localstorage and formatting them as dd-mm-yyyy instead of yyyy-mm-ddT00:00:00.000Z. ...

The effects of external CSS are not being displayed or are being overridden

I am currently working on a webpage design that resembles the style of Microsoft's website. I came across a tutorial on W3 Schools for creating an image slideshow, which can be found here: https://www.w3schools.com/howto/howto_js_slideshow.asp. The ex ...

Leveraging Vue's "v-slot" functionality to create a specified slot within a JavaScript object

I have turned to this platform seeking guidance on using "v-slot" while utilizing a syntax that involves a javascript object. This specific method was introduced in the online course Intro to Vue3 which I recently completed. Below is an image de ...

Implementing Codeigniter AngularJs routing with an initial base URL

I'm facing a challenge with routing in AngularJS to align with the base URL set in the config of CodeIgniter. Base URL = http://localhost:8080/example/ For instance, if my current URL is http://localhost:8080/example/details and there's an ...

Please replace the text with only letters and then submit it without any spaces, periods, commas, or other symbols

Whenever I encounter a form like this, I need to submit it and replace the text in the name field with only letters - no commas, spaces, or special characters. https://i.sstatic.net/9FQnT.png myform.html <script> function fixInput(event) { na ...

What is the best way to show a nested div element within a v-for loop in Vue.js?

One interesting feature of my coding project is that I have an array nested within another array, and I iterate through them using two v-for loops. The challenge arises when I try to display a specific div in the second loop only upon clicking a button. ...

Troubleshooting issues with Javascript ES6 module functionality

I'm struggling to solve a problem with my small app setup. In my project, I have an index.html file that includes a javascript file and another file named myJsModule.js in the same directory. Here's the code inside myJsModule.js: export default ...

Substitute the main node with the subordinate node within a json file

Here is a JSON object structure: { "coach": { "Errors": { "items": [ { "errorMessage": "You must select a date that is in the future.", "errorBOPath": "twl.date" ...

Switching Between Background Images in Angular

Looking to change the background-image of an element upon clicking it. Currently, this is what's in place: <div class="item-center text-center" toggle-class="active cable"> <div class="quiz-background"></div> <p class="size ...

Troubleshooting Issue: Click functionality not functioning for button within Bootstrap dropdown menu

Issue The event handler does not get triggered when a button is clicked inside a Bootstrap dropdown menu. Desired Outcome I want the event handler to execute when the button with id repeat_booking_button in the dropdown is clicked. Attempted Solutions ...

"Material-Table does not have the ability to insert a new row

Just started learning JS and React. I'm attempting to integrate Material-table with an add row button, but encountering issues where the added row is not reflecting. Every time I refresh the page, the rows are reset. I suspect there's a problem w ...

What is the method for defining an Angular directive within the link function of another directive?

I am in the process of developing a unique server-validate directive that verifies form input asynchronously by sending a partial form to our server backend for validation and analyzing the response. I envision using it in this manner: <input type="tex ...

What steps should I take to have a specific input prompt a certain action?

I am currently working on creating a function that checks when a user inputs a number between 0 and 8, triggering an action corresponding to that specific number. For example, if the user enters 5, a picture and text should appear; whereas if they input 3, ...

Including an anchor element with a specified URL, alongside passing the URL as a property

Having trouble passing a URL to href using a property. I'm attempting to pass the {props.github} value to href, but it's not working as expected. I've set up a property object with a field called github like this: export const projectList ...

tips for loading json data into jqgrid

Utilizing the struts2-jquery-jqgrid plugins, I have created a Grid with a filter-search feature. The Grid includes a drop-down list in the column assigned_user for filtering based on the selected option from the drop-down list. <s:url var="remoteurl" a ...

Strip the date after converting from milliseconds to a date

When my server back end sends the time value as milliseconds (1479515722195), I use a library function to convert it to a date format like Sat Nov 19 2016 11:35:22. Now, I need to figure out how to separate the date and time components. I only require th ...

What are the steps to send $http requests from AngularJS to a local server in an app?

Situation at Hand My goal is to perform an $http post request from Angular using the function below, defined in my controller: $scope.sendUserData = function(){ var userData = JSON.stringify({ 'firstName': $scope.firstName, ...

Utilizing static classes in Express: A guide

As a newcomer to the realm of JavaScript, I am eager to develop a web application using the MEAN stack. To maintain some level of organization in my files, I intend to encapsulate all MongoDb-related operations within a static class and call it from my ap ...