Angular.copy () does not copy all properties

Consider the following function in the service:

$scope.addPeriod = function(newPeriod) {        
        if(newPeriod.from !== '' && newPeriod.until !== '') {           
            var index = $scope.newPeriods.indexOf(newPeriod);       
            $scope.newPeriods.splice(index, 1); 
            $scope.setHolidayEditions(newPeriod);


            console.log("Check period");
            console.log(newPeriod); // state 1: object newPeriod is as expected, it contains holidayEdtions

            console.log("Check period copy");
            console.log(angular.copy(newPeriod)); //state 2 : object newPeriod is missing the holidayeditions!
            $scope.periods.push(angular.copy(newPeriod)); //original code
            //some more logic irrelevant to the question
        }
}

The problem is angular.copy (). The structure of the newPeriod object is as follows:

it has a date "from" and "to" and an array of journal objects. Then there is the function $ scope.setHolidayEditions (newPeriod) . This basically adds an array of instances for each log. This feature works. I know this because of the output to the console. In the source code, the period then begins in an array of periods, which is then displayed on the screen. angular.copy () was probably done to avoid link issues.

But angular.copy () does not seem to copy the newly created array of publications in the journal objects. Is there a reason for this?

This is basically what happens in the utility function:

newPeriod, :

{
  from:"02/10/2015", 
  until:"09/10/2015",
  magazines: [
           {title:"some title", number:"some number", code:"magazineCode"},
           {title:"other title", number:"other number", code:"magazineCode2"}
  ]
}

$scope.setHolidayEditions(newPeriod) :

    {
          from:"02/10/2015", 
          until:"09/10/2015",
          magazines: [
                   {title:"some title", number:"some number", code:"magazineCode", holidayEditions:["date","date","date"]},
                   {title:"other title", number:"other number", code:"magazineCode2", holidayEditions:["date","date","date"]}
          ]
}

angular.copy(newPeriod) :

 {
      from:"02/10/2015", 
      until:"09/10/2015",
      magazines: [
               {title:"some title", number:"some number", code:"magazineCode"},
               {title:"other title", number:"other number", code:"magazineCode2"}
      ]
    }

angular.copy() holidayEditions . , ?

+4
1

, . ( ) lodash, :

_. cloneDeep (, [customizer], [thisArg])

. , . customizer undefined, . thisArg ; (value [, index|key, object]).

: . , , Object, Object. , , DOM, Maps, Sets WeakMaps.

+6

Source: https://habr.com/ru/post/1609966/


All Articles