The form input ng-value appears to be populated, yet remains void of content

I'm working on an AngularJS application and encountering an issue with a form that is populated using ng-value.

The user fills in the input field, sees the text, but when they click the 'change' button, it suddenly becomes undefined. Any ideas on what might be causing this?

This is my controller code:

// This is fictional data; wineById will be a JSON object.
$scope.wineById = {"id":"4","name":"fdfvs1","percentage":"vcxz1"}

$scope.edit = function () {
    var wine = {
        id:$scope.wineById.id,
        name:$scope.name,
        percentage:$scope.percentage
    };

    console.log($scope.name, $scope.percentage);
};

HTML:

<form novalidate ng-submit="edit()">
        <label>ID of wine: <input type="text" ng-value="wineById.id" ng-model="id" ng-disabled="true"></label> <br>
        <label>Name of wine: <input type="text" ng-value="wineById.name" ng-model="name"></label> <br>
        <label>Percentage of wine: <input type="text" ng-value="wineById.percentage" ng-model="percentage"></label> <br>

        <input type="submit" value="Change">
</form>

Scenario 1:

The fields have existing input. Pressing the button without changing the data results in everything becoming undefined.

Scenario 2 (the one that works):

The fields have existing input. Changing all the fields with different data and pressing the button successfully updates everything.

Any suggestions on how to address scenario 1?

Answer №1

To maintain the functionality of ng-value in your HTML, make the following changes:

<form novalidate ng-submit="edit()">
  <label>ID wine:
   <input type="text" ng-init="id=wineById.id" ng-model="id" ng-value="wineById.id" ng-disabled="true">
  </label>
  <br>
  <label>Name wine:
    <input ng-init="name=wineById.name" type="text" ng-value="wineById.name" ng-model="name">
  </label>
  <br>
  <label>Percentage wine:
    <input ng-init="percentage=wineById.percentage" type="text" ng-value="wineById.percentage" ng-model="percentage">
  </label>
  <br>
  <input type="submit" value="Change">
</form>

For a more efficient approach, consider using only ng-model (unless two-way binding is not desired)

<form novalidate ng-submit="edit()">
    <label>ID wine:
      <input type="text" ng-model="wineById.id" ng-disabled="true">
    </label>
    <br>
    <label>Name wine:
      <input type="text" ng-model="wineById.name">
    </label>
    <br>
    <label>Percentage wine:
      <input type="text" ng-model="wineById.percentage">
    </label>
    <br>

    <input type="submit" value="Change">
</form>

Live Demo

Answer №2

There is no need to use ng-value when Angular already supports two-way binding with ng-model, making it sufficient for data binding.

<form novalidate ng-submit="edit()">
    <label>ID wine: <input type="text" ng-model="id" ng-disabled="true"></label> <br>
    <label>Name wine: <input type="text" ng-model="name"></label> <br>
    <label>Percentage wine: <input type="text" ng-model="percentage"></label> <br>

    <input type="submit" value="Change">
</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

Initiate the typeahead drop down menu by pressing the downward key and then navigate through the options within the drop down

Utilizing the bootstrap typeahead feature within my angular project has been a game-changer. I needed a way to activate the typeahead drop down by pressing the down key when no text had been entered yet. By following guidance from this resource, I was able ...

Is this method of connecting to mongodb in a node.js environment satisfactory?

As a beginner in programming, I am exploring different ways to access mongoDB within my code. One challenge I face is having the code required to load mongo included in every .js file. To overcome this, I aim to keep the database access code in just one fi ...

The Raycaster object in Three.js is not defined when trying to trigger a

I'm currently using a raycaster with mousemove to change the cursor and it's working really well! However, I've noticed a little issue - it seems to only work properly when the mouse is over either the model or the box itself. If I hover int ...

What causes the transparency in my three.js texture to malfunction unless some of it is outside the viewport?

Currently, I am attempting to apply a texture to a plane using a PNG file that contains transparency. However, after applying the texture, the transparent areas of the PNG appear black when viewing the shape. Interestingly, if any part of the image extend ...

Tips for validating input fields in Ionic to display errors after the user has finished filling them out

*Hello, I'm new to working with AngularJS and I'm currently trying to display an error message after the user has finished typing in the input field. However, I'm encountering an issue where the error message appears as soon as I start typin ...

Creating a Tic Tac Toe game using Javascript程序

As a beginner in programming, I am working on an assignment to create a Tic Tac Toe program using HTML and JavaScript. I found some code related to Tic Tac Toe on a website, but I am having trouble understanding how it works. Here is the code snippet: if ...

Restricted scope / Effective method for passing an array to the directive user

Picture this scenario where a custom directive is referenced in myHtml.tpl.html: <my-directive></my-directive> This directive starts with an isolated scope. Naturally, there's a controller tied to myHtml.tpl.html. I aim to pass a compu ...

Remove an item with AngularJS

I have a JSON file that contains data which I am able to display using AngularJS. The information is presented in rows with checkboxes and a Delete button. My goal is to remove the data from both the display and the JSON file. To delete, simply click on th ...

What is the best way to include all the selected items from a multi-select list when updating my view model?

I am currently developing a website using ASP.Net Core 2.1 MVC. Within one of my views, I have implemented two multi-select lists to enable users to assign individuals to a site. The left-hand list displays all unassigned users, while the right-hand list ...

How can I access a variable called 'person' that has not been defined? (using the basic map method in React JS)

My goal is to display the information stored in an object using the map method. Here's how I attempted it: <ul> {persons.map(person => <li key={person.id}> person.name </li>)} </ul> While there weren't any errors ...

Separate punctuation characters from alphanumeric texts using Javascript

I'm attempting to separate a string like the one below into an array of individual elements, for instance: var str = "(someword,bbb)" The resulting array should look like this: var arr = ["(", "someword", ",", ...

Is there a way for me to trigger an error within my Django form?

Currently, I have set up a PHP script on a server that responds with {"available":true} or {"available":false} based on the availability of an email address. The code used for this script is as follows: <?php $servername = "localhost"; $username = "ro ...

Obtaining a webpage link from a div element with JQuery

I have been working on developing my own lightbox gallery and I think I'm almost there. My gallery is currently set up as a grid, where each cell displays a preview image using an img tag with the url specified in the CSS. However, if I can't ex ...

Most efficient method for implementing a recursive function while looping through elements in an array

Let's consider a scenario where we have an array of letters: const letters = ['a','b','c','d']; In addition, we have a function that requires a string argument letter and generates a concatenated output of that ...

The global JavaScript variable is refusing to be posted during the DataTable Ajax request

I've been struggling to send an additional custom value to my MVC controller for a server-side data table. Despite trying various approaches, the value I want always ends up being null in my view model. Here is my code snippet: $(document).ready(func ...

Leveraging JavaScript functions for invoking Ajax requests, accompanied by ASP.NET controls

Having a background in PHP, I am accustomed to using a PHP file to handle all of my ajax calls. Recently, I have been introduced to ASP.NET controls and the overall environment. I am curious about the correct method for handling ajax requests when they n ...

The code base encountered a server error with response code 500 while running on a development server

I recently built a React Native application using the WebStorm IDE. The code I used was just the default boilerplate, but now I'm encountering an error. https://i.sstatic.net/UXwfI.png Is there anyone who can assist me with this issue? ...

What is the best way to enhance the capabilities of a current tinyMCE button?

I am interested in enhancing the capabilities of the default buttons in tinyMCE by adding additional functionality. I'm curious to know how this can be achieved. For instance, If I would like the paste button not only to paste content, but also perf ...

Creating a mind map: A step-by-step guide

I'm currently developing an algorithm to create a mind map. The key focus is on organizing the nodes intelligently to prevent any overlap and ensure a visually pleasing layout. Take a look at this snapshot (from MindNode) as an example: Any suggestio ...

Is there a way to efficiently update the CSS class attribute for all list items using just pure JavaScript?

I am looking to use pure Javascript, not jQuery, to access all elements within a <ul> list and remove the active class from every item except for the one selected in my menu. Shown below is the list: <ul id='flash-menu'> <li id=" ...