"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

What is the correct way to end this jQuery statement?

I've been working on this for about 6 hours now. I ran it through multiple Lint tools and various other online tests, but I just can't seem to get the statement below to close properly. There's a persistent error showing up on the last line ...

The combination of sass-loader and Webpack fails to produce CSS output

Need help with setting up sass-loader to compile SCSS into CSS and include it in an HTML file using express.js, alongside react-hot-loader. Check out my configuration file below: var webpack = require('webpack'); var ExtractTextPlugin = require ...

Error: TweenLite has not been recognized

/justincavery/pen/mPJadb - this is a link to CodePen After copying the code from CodePen and running it, I encountered an error: "Uncaught ReferenceError: TweenLite is not defined". The image only draws once and there is no animation unless I press "F5 ...

Creating identical dropdown filter options from API in React.js

My dropdown menu is experiencing an issue where it is showing duplicated values from the API. When I receive documents from the backend, some of them have the same name, resulting in duplication in my dropdown list. Here is a snippet of my code: The funct ...

The absence of 'www' in the ajax path is causing an error

Apologies for my poor English as I am currently a student. My issue is related to the use of ajax without "www" in the URL. Let me demonstrate. var path = "www.sinemayolu.com"; //Real-time Ajax Search $('.searchtext').keyup(function ...

Why is my Laravel Vue CRUD not automatically updating the posts section with new data?

Hey there, I've been working on a project using Laravel 5.6 with Vue.js for CRUD functionality. I'm trying to display the posts I've just added in the posts section without having to reload the entire page. However, my current code is only s ...

What is the best way to display SVGs from an API response using next/image or the <img> tag in Next.js?

I have a collection of SVGs being returned from an API response, which I am fetching inside the useEffect. The response structure is as follows: {svg1: 'https://remoteserver.com/assests/svg1.svg', ...and so on} (These SVGs are not static assets ...

Showing outcomes by making rapid consecutive AJAX requests

If you need a visual representation, I have a PHP script for a Hotel Management application that showcases a calendar (with columns representing days of the month and rows displaying prices per day). This script can be accessed in edit mode, where it gene ...

Utilize the $sample operator following the $group stage in MongoDB queries

Consider the dataset provided below: {company:"One", employee:"John"}, {company:"One", employee:"Mike"}, {company:"One", employee:"Donald"}, {company:"One", employee:"Mickey"}, {company:"Two", employee:"Johnny"}, {company:"Two", employee:"Da ...

What's the reason for the icon not updating in the responsive mode?

I am venturing into the world of responsive web design for the first time. My aim is to create a website menu that drops down when the menu icon is clicked, using the onclick command in JavaScript. However, upon inspecting my browser, I noticed an uncaught ...

What is the best way to incorporate a Thymeleaf message stored in message.properties into my JavaScript file?

Is there a way to insert messages from a message.properties file into a javascript file similar to how it's done in an HTML file? For example, on my home page I have a title listed as: <h1 th:text="#{home.screen.title}"></h1> Where home ...

CodeIgniter functionality for generating auto-incrementing IDs that are accessible in both the view and within JavaScript's Window.Print() method

While working on creating an invoice, I encountered a few issues. First, I want the Invoice No: to be displayed in my view (receipt.php) as 0001 and use it as the primary key in my tbl_payment table. However, I'm unsure how to have an auto-incremented ...

Steps to turn off a Material UI CSS class for an element universally

Utilizing the Material UI Typography element with the css class MuiTypography-h1, I am seeking to globally disable its usage throughout the entire codebase. <Typography variant="h1" sx={{ width: '100px', height: '55px ...

Fade or animate the opacity in jQuery to change the display type to something other than block

I am currently using display: table and display: table-cell to vertically align multiple divs. However, I have encountered an issue when animating the opacity with jQuery using either fadeTo() or fadeIn. The problem is that it always adds inline style di ...

Storing and Retrieving Mongoose Data Using Nested Schemas, References, and Promise Functions

I have a question regarding saving and retrieving documents with nested schema references in Mongoose. When I try to save a document with nested schema refs, and then retrieve it later, the required nested field is not included unless I populate it in the ...

Separate the information into different sets in JavaScript when there are more than two elements

Upon extraction, I have obtained the following data: ╔════╦══════════════╦ ║ id ║ group_concat ║ ╠════╬══════════════╬ ║ 2 ║ a ║ ║ 3 ║ a,a ...

Is the Vue-portal enabled conditionally?

I am looking to include some additional information in the navbar (parent component) using Vue Portal. So, within a component, I can use the following code: <portal to="navbar"> <b-button>Some option</b-button> </portal&g ...

Error encountered when running the production build server for android in Meteor: TypeError - Unable to access property 'slice' of null

Here's a demo I'm working on: https://github.com/pc-magas/pupAndroidDemo I'm trying to build this demo for Android. I've already installed the Android SDK and configured the necessary environmental parameters. Following the instructio ...

The slides on SlickJS are initially loaded in a vertical alignment, but once the arrows are interacted with,

I've put together a demonstration that highlights the issue I'm facing. Upon visiting the contact section, you'll notice that all the slickJS slides are stacked vertically initially, but once you interact with them by clicking on the arrow o ...

"Learn how to easily send media files stored on your local server via WhatsApp using Twilio with Node.js and Express

Whenever I attempt to send the PDF file created through WhatsApp, an error pops up preventing me from viewing it. easyinvoice.createInvoice(data, function(result) { //The response will contain a base64 encoded PDF file ...