javascriptangularjsangular-controller

DeepCopy in Angularjs?


Is there a way to add the $scope data to another $scope without changing the values from which it is added? Currently, I have created one button and whenever someones click on it the following action is performed.

    $scope.updateShiftSummary = function () {
        $scope.shiftModalSummary = $scope.shiftSummary;
        $('#updateShiftModal').modal('show');
    };

Since I am working on an update and whenever someone adds shifts following action is performed but if someone closes the modal without saving the form the $scope.shiftSummary also gets updated and the new data is also pushed in it so how to update the $scope.shiftModalSummary only without affecting the data in $scope.shiftSummary

 $scope.addShift = function () {
        var shiftSummary = $scope.shiftModalSummary;
        var totalShifts = shiftSummary.length;
        var lastShift = shiftSummary[totalShifts - 1].shiftName.split(" ");
        var shiftValue = parseInt(lastShift[1]) + 1;
        var shiftTime = new Date(new Date().setHours(0, 0, 0, 0));
        shiftSummary.push({
            'shiftName': 'Shift ' + shiftValue,
            'startTime': shiftTime,
            'endTime': shiftTime,
            'staffName': 'Owner'
        });
    };

Solution

  • $scope.updateShiftSummary = function () {
        $scope.shiftModalSummary = angular.copy($scope.shiftSummary); <---
        $('#updateShiftModal').modal('show');
    };