Angular bootstrap ui modal binding scope to parent

I have a problem with Angular to get a modal unbound from the parent area. I want the scope object I passed to the modal to be decoupled from the corresponding scope object.

No matter how I structure the modal, the parent always reflects it. The only solution I found was to change the object property names, but that would be cumbersome for every modal in my project.

For example, I can have a $ scope variable in the parent $ scope.parentData.firstName and a modal variable $ scope.modalData.abcfirstName, and the parent will display the modal value.

I am guessing there are some problems with the parent $ scope object here that I am not getting. Here's an illustration of the problem:

http://plnkr.co/edit/5naWXfkv7kmzFp7U2KPv?p=preview

HTML:                                                 

<div ng-controller="ModalDemoCtrl">
    <script type="text/ng-template" id="myModalContent.html">
        <div class="modal-header">
            <h3>I'm a modal!</h3>
        </div>
        <div class="modal-body">
                <input ng-model="modalData.a" />
                <input ng-model="modalData.b" />
                <input ng-model="modalData.c" />
            Selected: <b>{{ sourceData }}</b>
        </div>
        <div class="modal-footer">
            <button class="btn btn-primary" ng-click="ok()">OK</button>
            <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
        </div>
    </script>

    <button class="btn btn-default" ng-click="open()">Open me!</button>
    {{sourceData}}
    <div ng-show="sourceData">Selection from a modal: {{ test }}</div>
</div>
  </body>
</html>

      

JS:

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

  $scope.sourceData = {a:'abc',b:'bcd',c:'cde'};

  $scope.open = function () {

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

    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.modalData = {};
  $scope.modalData = data;

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

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

      

+3


source to share


1 answer


You pass a reference to your current object, what you want to do is use angular.copy()

to pass a deep copy of the object to the modal plnkr :



var modalInstance = $modal.open({
  templateUrl: 'myModalContent.html',
  controller: ModalInstanceCtrl,
  resolve: {
    data: function () {
      return angular.copy($scope.sourceData); // deep copy
    }
  }
});

      

+6


source







All Articles