"Retrieving the most recent data within an ng-repeat directive using AngularJS

I am facing an issue with ng-repeat in my application. It successfully fetches values from the database and displays them in input text fields. However, when I update these values and click the save button, the updated values are not being saved. Can someone please provide guidance on resolving this issue?

<div ng-controller="education" ng-init="getEducationDetails();">
    <div class="col-md-12">
                    <div class="row" ng-repeat="values in education"
                        style="margin-left: 20px; margin-right: 20px; margin-top: 10px;">
                        <div class="col-md-6">
                            <h4>
                                <small>Degree</small>
                            </h4>
                            <input type="text" id="educationDegree" name="educationDegree"
                                value="{{values.education_degree}}" style="width: 90%;" />
                        </div>
                        <div class="col-md-6">
                            <h4>
                                <small>Year</small>
                            </h4>
                            <input type="text" id="educationYear" name="educationYear"
                                value="{{values.education_year}}" style="width: 100%;" />
                        </div>
                        <div class="col-md-12" style="margin-top: 10px;">
                            <h4>
                                <small>University</small>
                            </h4>
                            <input type="text" id="educationUniversity"
                                name="educationUniversity"
                                value="{{values.education_university}}" style="width: 100%;" />
                        </div>
                    </div>
                </div>
                <div class="col-md-12" style="margin: 20px;">
                    <button ng-click="educationSave();" class="btn btn-default">Save</button>
                </div>
</div>

After clicking the Save button, I use the educationSave() method to save the values. However, I noticed that the education array within the method contains the old values instead of the updated ones. How can I ensure that it captures the latest changes made to the input fields?

Below is how I retrieve the values:

$scope.getEducationDetails = function()
            {
                $http({
                    method : 'GET',
                    url : '/getEducationDetails'
                }).success(function(data, status, headers, config) {
                        $scope.education = data.resultSet;
                        alert($scope.education);
                }).error(function(data, status, headers, config) {
                });
            };

And here is my controller method for saving the values:

    $scope.educationSave = function() {
        var educationArray = JSON.stringify($scope.education);
        //The educationArray is then saved
    };

Answer №1

The issue at hand is the lack of utilization of ng-model to connect your input value with the controller. According to the official documentation:

The HTML input element control. When paired with ngModel, it enables data-binding, input state management, and validation.

For each input in your markup, consider implementing something similar to the following:

<input type="text" id="educationDegree" name="educationDegree"
                            ng-model="values.education_degree" style="width: 90%;" />

Check out this quick Demo

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

Retrieving information from a dynamically generated HTML table using PHP

I have successfully implemented functionality using JavaScript to dynamically add new rows to a table. However, I am facing a challenge in accessing the data from these dynamically created rows in PHP for database insertion. Below, you will find the HTML ...

Every time I attempt to reuse my components, they keep piling up on top of each other

I'm facing an issue where I need to reuse components I've created multiple times while rendering dynamic content. However, when I attempt to render them, they end up stacking on top of each other in the same position. Each time I render ...

Leverage the power of integrating Power BI with Javascript to easily retrieve an

I have embarked on a mission to integrate PowerBI into my web application. Below is the code snippet that I have implemented: window.config = { instance: 'https://login.microsoftonline.com/', tenant: 'common', //COMMON OR YOU ...

Getting JSON data from an API using $.ajax

Currently, I am working on creating a random quote machine. To start off, I wrote the following HTML code to outline the necessary elements: <div id="quoteDisplay"> <h1 id="quote">Quote</h1> <h2 id="author">- Author</h2> ...

React JS's popup feature can be unreliable, as it tends to break the application frequently

I have been developing a web application that interacts with a public API and presents the retrieved data in a table format. However, I am encountering an issue where clicking the "Click to Display Hottest Planet" button sometimes results in an error. How ...

Issue with Jest Test Trigger Event Not Invoking Method

Currently, I am in the process of writing tests for my Vue application. One particular test involves a button that triggers a logout function. The goal is to determine if the function is executed when the button is clicked. Initially, I attempted to mock ...

Customize node.js response by overriding or adding another return statement

I have two variables, FirstName and LastName, that I need to include in the response data. It is important to note that the FirstName and LastName are not stored in the Student table. let result = await Student.get(id) let FirstName = "Paul" let LastName ...

Numerous Controllers in AngularApp failing to function properly

One issue I'm encountering is that only one out of the two controllers within my app seems to be registered: var app = angular.module('MyModule', []); app.controller('FruitCtrl', function($scope) { $scope.fruits = ['appl ...

Accessing JSON data and populating a dropdown menu with JQuery

I am facing an issue with extracting values from a JSON array using jQuery. The structure of the array is as follows: [{ "": "Select your State/Region" }, { "BAL": "Balkh" }, { "BAM": "Bamian" }] Although I have attempted to loop through the array in ord ...

Issue: A SyntaxError is triggered due to an unexpected termination of the JSON input while utilizing the fetch()

I encountered an issue while working on sending JSON between a Go API server and a React front-end. The error message I received was: Error: SyntaxError: Unexpected end of JSON input The specific line where the error is occurring is Line 25, which contai ...

Creating a versatile menu component: A step-by-step guide

One of my current projects involves designing a menu screen component that must be user-friendly and easily customizable. The component will consist of various options displayed in list form. const options = [ { text: 'Option 1', }, { ...

When using ReactJS, hovering over a row in a table should trigger a change in the background color of the corresponding row in another table with the same index

I need to create a feature where hovering over a row in the first table will highlight a corresponding row in the second table. The highlighting should be based on the index of the hovered row. Here is an example: <div> <table> < ...

I am looking to preload a separate webpage prior to the HTML loading in AngularJS

I am looking to implement a feature in my AngularJS app where a different webpage (e.g. google.com) will be loaded based on the response of a REST API before any default HTML content is displayed. I have attempted to make a REST service call within a fact ...

The MomentJS .format() function accurately displays the date as one day in the past in my local time

After using momentJs to display a date in a specific format in my UTC-4:30 timezone, I noticed that it skips a day. I am currently in the UTC-4:30 timezone. This issue does not occur in all timezones; it works correctly in the UTC-5:00 timezone. The fol ...

Encountering a NaN outcome when summing values from various select options

I'm working on a project that involves adding up the prices based on the surface chosen by the user. I'm struggling with calculating the partial cost when the user's choice changes. totalSum.ts num: Calculation totalAmount: number cate ...

How can AngularJS handle uploading multipart form data along with a file?

Although I am new to angular.js, I have a solid understanding of the fundamentals. My goal is to upload both a file and some form data as multipart form data. I've learned that this is not a built-in feature of angular, but third-party libraries can ...

Issue encountered in Angularjs during upgrade process from version 1.2.27 to 1.4.7

Recently, I upgraded my app from using AngularJS 1.2.27 to version 1.4.7, but now I am encountering an error in the AngularJS file. SyntaxError: Unexpected token : (angular.js:12477) at Function (native) at Object.sd. ...

"Utilizing the ui-grid feature to dynamically apply cell filters

I am looking to utilize ui-grid to create a column with a dynamic cellFilter that can be updated on the fly, from 'number' to 'currency', for example. I attempted changing the parameter cellFilter within the column but it does not refle ...

The JavaScript promise remains in limbo, neither resolving nor rejecting, seemingly stuck for unknown reasons

In the process of developing a node script, I encountered an issue where the images were not being ordered according to the calculated score value. The score is determined by a function named getImageScore(), which unfortunately takes a considerable amount ...

Incorporate a JavaScript array into a TypeScript document

Having a file named array.js with a large collection of strings, structured like this: module.exports = ["word1","word2",...] I attempted to utilize this array in my validation.ts file by adding the following line: let wiki = require('./array.js&a ...