Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

javascript - 在AngularJS中更改嵌套对象的值会更改所有其他值(Changing nested object value in AngularJS changes all other values)

Consider the following nested object that loops 10 times to product a object with schedules for 10 weeks, for every monday at the given times:

(考虑以下嵌套对象,该嵌套对象循环10次,以在给定时间的每个星期一循环生成10周时间表的对象:)

        $rootScope.bookings_times_list = [];
        for (i = 0; i < 10; i++) {    
            $rootScope.bookings_times_list.push({
                week: i,
                schedule: [{
                            booking_day: "Monday",
                            booking_times: [{
                                                "booking_start_time": "00:15",
                                                "booking_end_time": "01:00",
                                                "booking_selected": "",
                                                "booking_selected_date": "",
                                                "booking_time": "30 Nov 2019 07:35hrs"
                                            }]
                            }]                         
                });
        }

The problem is that changing the booking_selected value to 1 as shown below changes the values for all booking_selected keys for every week to 1 :

(问题在于,如下所示将booking_selected值更改为1 ,将每周所有booking_selected键的值更改为1 :)

$rootScope.bookings_times_list[0].schedule[0].booking_times[0].booking_selected = 1;

Why would this happen when the key for $rootScope.bookings_times_list[0] is clearly set to 0 ?

(当$rootScope.bookings_times_list[0]的密钥明确设置为0时,为什么会发生这种情况?)

  ask by Dakarai Mshoperi translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...