Using AngularJS to refresh information stored in an array

My objective is to create a basic application that allows users to adjust their availability on weekdays. The code is functioning correctly as it retrieves data from the select box. However, I encounter an issue when trying to update Monday's data and find that Tuesday's data is also being updated with the same values.

Here is my JSON database:

"doctorSchedule" : [ 
        {
            "_id" : null,
            "working_day" : "Monday",
            "working_from" : [ 
                {
                    "_id" : null,
                    "hours" : "9",
                    "minutes" : "30"
                }
            ],
            "working_to" : [ 
                {
                    "_id" : null,
                    "hours" : "6",
                    "minutes" : "30"
                }
            ]
        }, 
        {
            "_id" : null,
            "working_day" : "Tue",
            "working_from" : [ 
                {
                    "_id" : null,
                    "hours" : "9",
                    "minutes" : "30"
                }
            ],
            "working_to" : [ 
                {
                    "_id" : null,
                    "hours" : "6",
                    "minutes" : "30"
                }
            ]
        }
    ],

Below is my Angular code:

$scope.data1 = $stateParams.viewUser;
$http({ 
    url: "/getinfo",
    method: "POST",
    headers :{'Content-Type': 'application/json','Accept': 'application/json'},
    data: dataParam    
}).success(function(response) { 
    if(response.status_code == "worked") {
      $scope.viewUser = response.clinicUserVo;
      $scope.datasc = response.doctorSchedule;                              
    } 
});

My HTML file includes the following components:

<table class="table table-bordered" ng-repeat="docSchedule in data1.doctorSchedule">
<tr>  <th scope="col"><input type="checkbox"  ng-model="data1.working_day"></th>
                                            <td>{{docSchedule.working_day}}</td>
                                            <td>

...
(more HTML code here)
...

                                        </tr>

                                 <tr>

</tr>
<tr >


</table>

I am experiencing issues where updates made to Monday also affect Tuesday's data in the HTML view. How can this be resolved using Angular?

https://i.stack.imgur.com/PIvLg.png

Answer №1

Adjust

<select  id="user_time_zone"   class="form-control form-group" ng-model="data1.working_from">

to

<select  id="user_time_zone"   class="form-control form-group" ng-model="docSchedule.working_from">

This modification ensures that each select within every ng-repeat will reference the correct docSchedule data.

Got it? In simpler terms, your current code is not functioning as intended because data1.working_from is pointing to the same model across all scopes and ng-repeat's.

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

Issues encountered when using Swift with jsonbin API

I've been working on a small app that communicates with the jsonbin API using Alamofire. While their API documentation is thorough when it comes to using Python for "putting" or "updating" JSON file data, I'm currently tackling this task in Swift ...

Utilizing Cookies in an AJAX Request for Cross-Domain Communication with Pure Javascript

I am in the process of developing an Analytics application and I'm seeking a method to uniquely identify each user's device. Currently, my approach involves generating a "cookie" from the server side to track all page clicks and interactions thro ...

encountering a problem with retrieving the result of a DOM display

private scores = [] private highestScore: number private studentStanding private studentInformation: any[] = [ { "name": "rajiv", "marks": { "Maths": 18, "English": 21, "Science": 45 }, "rollNumber": "KV2017-5A2" }, { "n ...

The JSONObject's starting brace must be at the first character of the first line to avoid the '}' error

String JSON = "http://www.json-generator.com/j/cglqaRcMSW?indent=4"; JSONObject jsonObject = new JSONObject(JSON); JSONObject extractedObject= jsonObject.getJSONObject("extract"); Object item = extractedObject.get("2"); System.out.println(item); I' ...

Discover the ultimate guide to efficiently extracting data from extensive JSON files using either the powerful combination of regular expressions and the Json Path Extract

I have the following JSON body. How can I extract the values of StudentId, TermID, and other fields? Here is the JSON body: [{"Key" : "Results", "ResultsCollection" : [{"Key" : "IS.S2.SES.FSA.LoadStudentProfile('51483081','HCOL',' ...

Exploring Appsetting Configuration in AppModule of Angular 8

I'm looking to update my configuration in the appsettings file by replacing a hardcoded string with a reference to the appsetting. Currently, I have this hardcoded value in appmodule.ts: AgmCoreModule.forRoot({ apiKey: 'testtesttest', li ...

How can you utilize data captured through a JavaScript onchange event in conjunction with another event?

Is there a way to utilize the firmType data captured during event 1 in event 2? code event 1 $("body").on("change","#so_customer", function(v){ customerFirm = $(this).find(":selected").data("employer"); $("#customer_employer").val(cust ...

Selecting the most popular post from a Facebook group

Here is the JSON file that I am currently using JSON.parse(); in Google Apps Script. Currently, I have found a temporary solution with this code by selecting posts that have more than 15 likes. However, my goal is to be able to select the post with the ...

Encountering issues with calling a custom directive within another custom directive in AngularJS

I need to implement a custom directive within the template of another custom directive. Here are the code snippets for reference: Issue with Scenario 1 angular.module('myApp') .directive('customOnChange', function () { return { r ...

apply a course to the designated element

Alright, I have this piece of code that deals with session and page requests. // Let's start our session session_start(); // Now let's check if there is a page request if (isset($_GET['page'])) { // If there is a requested page, we ...

Retrieving information from an object using JavaScript

Hello, I am facing a challenge with extracting data from an object via a web API in ReactJS. It seems like the data returned by the API is not structured properly as a JavaScript object. To view the issue directly in your browser visit: I need assistance ...

Executing Actions Prior to Redirecting

I am working on implementing a timer process using a custom JQuery plugin that will redirect to the login page after a specific number of minutes. Additionally, when the timer reaches zero, I need to execute a task, which in this case involves making a cal ...

Stop the scrolling behavior from passing from one element to the window

I am facing an issue with a modal box window that contains an iframe. Inside the iframe, there is a scrollable div element. Whenever I try to scroll the inner div of the iframe and it reaches either the top or bottom limit, the browser window itself start ...

storing information in HTML using Django

Recently, I have been learning about Django Rest and I am now trying to display some JSON data in HTML. The JSON data I have is: {'Resul': {'Period Start': '2017-01-01', 'Period End': '2017-12-12'}} When ...

What are the steps for utilizing JSON data retrieved from an ajax response?

After successfully making an ajax request and receiving JSON data, I am struggling with how to use it effectively. The response I am getting looks like this: [{ "id": "5", "reviewID": "2389", "serviceID": "50707", "title": "well d ...

Utilizing AngularJS uib-collapse to Toggle Visibility of Table Columns

Currently, in Bootstrap the uib-collapse class animation only works for vertical divs. However, I am looking to implement the same animation/motion behavior for table columns when showing or hiding them upon clicking on an icon. Right now, I am using ng-s ...

Navigating through root and sub applications within AngularJS can be managed effectively when housed in sub directories

I currently have two AngularJS applications in place: The first application serves as the base and operates at the root / The second application is a sub-application that needs to be isolated for design purposes, running within a subfolder such as /foo/ ...

Discover the process of accessing nested arrays in MongoDB

I am facing a challenge with retrieving patient_ids from nested json array where the code value is M18.1 using a mongo db query. {"zip":"04903","specialty":"cardiac","number_of_patients":250,"data":[{"gender":"male","age_data":[{"age":1,"diagnostic_dat ...

What could be the reason why my useRouter function isn't working as expected?

I'm currently working on developing an App using nextjs 13 and the new App router feature (https://nextjs.org/blog/next-13-4) : I've encountered a navigation issue despite following the documentation diligently. To illustrate, here's a bas ...

Tips for eliminating null elements from an array with the help of jquery

Similar Question: How to delete empty values from an array using JavaScript? I'm looking for a way to eliminate null or empty elements from an array with the use of jquery var clientName= new Array(); clientName[0] = "jack"; clientName[1] = ""; ...