【问题标题】:Accessing angular bootstrap modal form from controller从控制器访问角度引导模式形式
【发布时间】:2014-01-09 21:45:36
【问题描述】:

我正在尝试从模态控制器 (Plunkr) 访问表单,但似乎无法访问 myForm。如何获得提醒上班:

angular.module('plunker', ['ui.bootstrap']);
var ModalDemoCtrl = function ($scope, $modal, $log) {
  $scope.open = function () {
    var modalInstance = $modal.open({
      templateUrl: 'myModalContent.html',
      controller: ModalInstanceCtrl          
    });       
  };
};

var ModalInstanceCtrl = function ($scope, $modalInstance) {
  $scope.submit = function() {
    // How to get this?
   alert($scope.myForm.$dirty);
  };

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

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

和模板:

<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">
          <form name="myForm" novalidate>
          <input type="email" value="hello">
          </form>
        </div>
        <div class="modal-footer">
            <button class="btn btn-primary" ng-click="submit()">OK</button>
            <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
        </div>
    </script>

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

【问题讨论】:

    标签: javascript angularjs twitter-bootstrap


    【解决方案1】:

    Angular-UI 模态使用嵌入来附加模态内容,这意味着在模态中创建的任何新范围条目都是在子范围中创建的。这发生在表单指令中。

    您可以尝试使用 (Angular 1.2.16) 将表单附加到父范围:

    <form name="$parent.userForm">
    

    userForm 在 modal 的控制器 $scope 中创建并可用。感谢范围继承 userForm 访问在标记中保持不变。

    <div ng-class="{'has-error': userForm.email.$invalid}"}>
    

    【讨论】:

      【解决方案2】:

      ui-bootstrap 中的known bug所以注入 $modalInstance 现在可以正常工作了。

      解决方法是将表单实例显式传递给提交函数:

      <form name="myForm" novalidate ng-submit="submit(myForm)">
        <div class="modal-header">
          <h3>I'm a modal!</h3>
        </div>
        <div class="modal-body">
          <input type="email" value="hello">
        </div>
        <div class="modal-footer">
          <button class="btn btn-primary" type="submit">OK</button>
          <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
        </div>
      </form>
      
      var ModalInstanceCtrl = function ($scope, $modalInstance) {
        $scope.submit = function(myForm) {
         alert(myForm.$dirty);
        };
      };
      

      【讨论】:

      • 这已在 ui-bootstrap 0.12 中修复。不再需要解决方法。
      【解决方案3】:

      在你的控制器顶部添加:

      $scope.form = {};
      

      在html模板中:

      <form name="form.yourFormName">
      

      验证 html 元素:

      <div ng-class="{'has-error': form.yourFormName.email.$invalid}"}>
      

      另见:

      AngularJS modal dialog form object is undefined in controller

      【讨论】:

      • 不错的解决方法 - 防止 angular 在嵌入范围内创建表单对象,因为它已经从父级继承。
      • 嗯——除非你的控制器的作用域也继承自父作用域,那么你可能会覆盖一个已经继承的表单。我认为这样会更好:$scope.form = $scope.form || {}
      • 我的意思当然是“隐藏”,而不是“覆盖”——在大多数情况下这不太可能成为问题,但你永远不知道......
      【解决方案4】:

      最后我选择了gertas给出的答案,但以下是解决这个问题的另一种方法。

      //place a method on modal controller scope
      
              $scope.setFormReference = function (myForm) {
                  $scope.myForm = myForm
              };
      
      //call this using an angular expression in your modal template
      
             {{ setFormReference(loginForm)}}
      

      【讨论】:

        猜你喜欢
        • 2015-04-14
        • 2016-10-24
        • 1970-01-01
        • 1970-01-01
        • 2013-10-30
        • 1970-01-01
        • 2014-11-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多