4

I've managed to use the example from angular-ui-bootstrap to demonstrate the problem (but you need the console open to see it) Plnkr link to this code

Why is the form being stuck in a sub scope instead of this $scope?

The HTML

<!doctype html>
<html ng-app="plunker">
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js"></script>
    <script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.10.0.js"></script>
    <script src="example.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>

<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">
          <ng-form name="myForm">
            <ul>
                <li ng-repeat="item in items">
                    <a ng-click="selected.item = item">{{ item }}</a>
                </li>
            </ul>
            Selected: <b>{{ selected.item }}</b>
          </ng-form>
        </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>
    <div ng-show="selected">Selection from a modal: {{ selected }}</div>
</div>
  </body>
</html>

** The Javascript **

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

  $scope.items = ['item1', 'item2', 'item3'];

  $scope.open = function () {

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

    modalInstance.result.then(function (selectedItem) {
      $scope.selected = 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, items) {

  $scope.items = items;
  $scope.selected = {
    item: $scope.items[0]
  };

  $scope.ok = function () {
    console.log("The form in the $scope says: ", $scope.myForm);
    console.log("The form in the $scope.$$childTail says: ", $scope.$$childTail.myForm);
    $modalInstance.close($scope.selected.item);
  };

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

The ng-form is not in the $scope where I would expect it to be, even though the other $scope items are there. What is the cause of the nested scope for the form (that is added by angular for form validation).

The output from the console looks like:

The form in the $scope says:  undefined example.js:37
The form in the $scope.$$childTail says:  
Constructor {$error: Object, $name: "myForm", $dirty: false, $pristine: true, $valid: true…}
Khanh TO
  • 48,509
  • 13
  • 99
  • 115
boatcoder
  • 17,525
  • 18
  • 114
  • 178
  • 1
    You may look at a solution here http://stackoverflow.com/questions/19312936/angularjs-modal-dialog-form-object-is-undefined-in-controller – Khanh TO Mar 01 '14 at 04:48

3 Answers3

3

If you do not specify a scope property on $modal.open it take $rootscope. This is from documentation

scope - a scope instance to be used for the modal's content (actually the $modal service is going to create a child scope of a provided scope). Defaults to $rootScope

so set scope before call

 var modalInstance = $modal.open({
      templateUrl: 'myModalContent.html',
      controller: ModalInstanceCtrl,
      scope:$scope,
      resolve: {
        items: function () {
          return $scope.items;
        }
      }
    });
Chandermani
  • 42,589
  • 12
  • 85
  • 88
  • So what is the $scope that is injected to the modal controller? Is that the same $scope as passed in the open? – boatcoder Mar 01 '14 at 05:12
  • This would be the $scope object of parent controller in your case `ModalDemoCtrl` – Chandermani Mar 01 '14 at 05:21
  • This isn't exactly the question i was asking, I was trying to understand why there was an extra scope in the dialog instead of the one I created the scoped variables in. Turns out @Khanh TO was right, the answer was at http://stackoverflow.com/questions/19312936/angularjs-modal-dialog-form-object-is-undefined-in-controller The extra scope comes from the transclude that is done by the modal dialog. – boatcoder Mar 02 '14 at 01:16
2

If you want to access the $scope declared on your main controller and make it accessible to the modal you have to include in your modal configuration.

   scope: $scope

Just like what Chandermani put up on his answer above.

MarkJ
  • 867
  • 10
  • 13
2

This is because transclusion used in modals which creates new scope for form. I am defining forms this way:

<form name="$parent.myForm">

Then you can use $scope.myForm in modal controller.

gertas
  • 16,869
  • 1
  • 76
  • 58