为什么$ modal为表单引入了额外的范围?

时间:2014-03-01 04:11:35

标签: angularjs angular-ui-bootstrap

我设法使用angular-ui-bootstrap中的示例来演示问题(但是您需要打开控制台才能看到它)Plnkr link to this code

为什么表单被卡在子范围而不是$ scope?

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>

** 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');
  };
};

ng-form不在我预期的$ scope中,即使其他$ scope项目在那里。表单嵌套作用域的原因是什么(由表单验证添加角度)。

控制台的输出如下:

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…}

3 个答案:

答案 0 :(得分:3)

如果您未在$modal.open上指定范围属性,则需要$ rootscope。这来自文档

  

范围 - 用于模态内容的范围实例(实际上   $ modal服务将创建一个提供的子范围   范围)。默认为$ rootScope

所以在调用之前设置范围

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

答案 1 :(得分:2)

如果要访问主控制器上声明的$ scope并使其可供模式访问,则必须包含在模态配置中。

   scope: $scope

就像Chandermani在上面回答的那样。

答案 2 :(得分:2)

这是因为在模式中使用的转换为表单创建了新的范围。我这样定义表单:

<form name="$parent.myForm">

然后你可以在模态控制器中使用$scope.myForm