Drigger_1618 Drigger_1618 - 1 month ago 8
AngularJS Question

How to create a new instance of variables in AngularJs so that their values would be independent of the original one?

I am trying to populate a field in a map with a another map from a list of pre-defined maps. I am using the following code to do so.

$scope.parentMapList=[
{
fields:[]
},
{
fields:[]
}
];
$scope.fieldsList=[
{..},
{..}
];
$scope.addField = function(parentIndex,fieldIndex){

$scope.parentMapList[parentIndex].fields.push($scope.fieldsList[index]);

};


And I am using the parentMapList as ng-models in the view. But whenever I edit one of the fields in one of the map. It automatically edits an another field of the same category in another map.

How do I decouple/remove the binding between them ? Is there a possibilty to create a new instance everytime I try to copy one of the fields into another in angularJS.

Answer

Use var data= angular.copy($scope.parentMapList);

Please refer the link

Comments