2

Sometimes, when two variables refer to the same variable, they are not binding together like this:

  var option = 1;
  $scope.a = option;
  $scope.b = option;

When you change $scope.a, $scope.b does not change. See this Plunker

However, sometimes they are binding together, for example it happens to me in a modal like this:

angular.module('plunker', ['ui.bootstrap']);
var ModalDemoCtrl = function($scope, $modal, $log) {

  $scope.testDataArray = [{
    "name": "Doe"
  }, {
    "name": "Deer"
  }, {
    "name": "Female"
  }];


  $scope.open = function(testData) {

    var modalInstance = $modal.open({
      templateUrl: 'myModalContent.html',
      controller: ModalInstanceCtrl,
      resolve: {
        data: function() {
          return testData;
        }
      }
    });

    modalInstance.result.then(function(selectedItem) {
      $scope.scopeData = selectedItem;
    }, function() {
      $log.info('Modal dismissed at: ' + new Date());
    });
  };
};

// Please note that $modalInstance represents a modal window (instance) dependency.
// It is not the same as the $modal service used above.

var ModalInstanceCtrl = function($scope, $modalInstance, data) {
  $scope.modalData1 = data;
  $scope.modalData2 = data;

  $scope.ok = function() {
    $modalInstance.close($scope.modalData);
  };

  $scope.cancel = function() {
    $modalInstance.dismiss('cancel');
  };
};

See the Plunker. Here "modalData1" and "modalData2" are both referring to "data". In any modal of this Plunker, you change modalData1, then modalData2 changes with it.

Why it is like this??

Thank you!

2
  • 2
    In the first case, the common variable being assigned is an integer, which is a value type. In the second case, it is an object, which is a reference type. That means that in the first case, you are assigning the value of 1 to both scope variables so there is not link to each other, whereas in the other case, you are not duplicating the data object, but rather assigning a reference to the original object; thus when the object changes, the references reflect that. Commented Jul 21, 2015 at 19:58
  • 1
    I see. So when it comes to object, I need always use angular.copy() in order to cut the binding? Commented Jul 21, 2015 at 20:12

3 Answers 3

4

UPDATE:
You should copy the element, because since javascript is pass-by-reference both of this variables (references) are in fact pointing to the same object. If you want to remove that side effect you have to copy object before assigning:

$scope.modalData1 = angular.copy(data);
$scope.modalData2 = angular.copy(data);

plunker

ORIGINAL ANSWER:

In example you've provided modalData2 input is changing with modalData1, because they have the same model assigned in ng-model attribute:

<div class="modal-body">
    modalData1:
    <input ng-model="modalData1" />
    <br>
    modalData2:
    <input ng-model="modalData1" />
</div>   

When I fix it, then they are independent (plunker):

<div class="modal-body">
    modalData1:
    <input ng-model="modalData1" />
    <br>
    modalData2:
    <input ng-model="modalData2" />
</div>   

Then when you update modalData1 input, then the other does not change.

Sign up to request clarification or add additional context in comments.

Comments

2

Simple data types like the number 1 (var option = 1: handled as an integer) you used are copied by default. Objects or functions on the other hand are passed by reference, so their contents will seem to bind together.

Comments

0

Its because angularjs supports two-way binding which is being done here when you use ng-model which is directive provided by angularjs and since you are binding both the input fields with the same ng-model value changing one reflects on the other.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.