加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 17:20:04 所属栏目:安全 来源:网络整理
导读:我已经设法使用angular-ui-bootstrap中的示例来演示问题(但是你需要打开控制台才能看到它) Plnkr link to this code 为什么表单被卡在子范围而不是$scope? HTML !doctype htmlhtml ng-app="plunker" head script src="http://ajax.googleapis.com/ajax/libs
我已经设法使用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项目在那里.表单嵌套范围的原因是什么(由表单验证添加为angular).

控制台的输出如下所示:

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

解决方法

如果你没有在$modal.open上指定一个范围属性,那就需要$rootscope.这是来自文档

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

所以在通话前设定范围

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读