当前位置: 首页 > 面试题库 >

在控制器中未定义AngularJS模态对话框表单对象

通远
2023-03-14
问题内容

我们有一个页面打开一个模态对话框,其形式如下。但是,当我们点击应该处理表单动作的控制器时,表单对象是未定义的,并且我太过喜欢Angular新手了,不明白为什么…

这是父页面控制器持有的用于打开模式对话框的功能:

app.controller('organisationStructureController', ['$scope', ..., '$modal', function ($scope, ..., $modal) {

    $scope.openInvitationDialog = function (targetOrganisationId) {
      $modal.open({
          templateUrl: 'send-invitation.html',
          controller: 'sendInvitationController',
          resolve: {$targetOrganisationId: function () {
            return targetOrganisationId;
          }
          }
        }
      );
    };

在这样的页面上:

// inside a loop over organisations
<a ng-click="openInvitationDialog({{organisation.id}})">Invite new member</a>

邀请对话框html如下所示:

    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <!-- ... -->
            </div>
            <div class="modal-body">
                <form name="invitationForm">

                    <div class="form-group">
                        <label for="email" style="color:white;">Email</label>
                        <input type="email" class="form-control"  autocomplete="off" placeholder="New member email" id="email" name="email" ng-model="invitation.email" required="true"/>
                        <span class="error animated fadeIn" ng-show="invitationForm.email.$dirty && invitationForm.email.$error.required">Please enter an email address!</span>
                        <span class="error animated fadeIn" ng-show="invitationForm.email.$error.email">Invalid email</span>
                    </div>

                    <!-- ... -->

                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" ng-click="cancel()">Cancel</button>
                        <button type="submit" class="btn btn-primary" ng-click="sendInvitation()">Invite</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

应该处理邀请的控制器在其他地方:

  app.controller('sendInvitationController', ['$targetOrganisationId', '$scope', ...,
    function ($targetOrganisationId, $scope, ...) {

    $scope.invitation = {
      // ...
      targetOrganisation: {
        id: $targetOrganisationId
      }
    };

    $scope.sendInvitation = function () {

      // $scope.invitationForm is undefined
      if ($scope.invitationForm.$invalid) {
        return false;
      }

      // send the invitation...

    };
  }]);

那么将表单范围带入控制器的正确方法是什么?

也许我需要注入$modal并将其sendInvitationController添加到sendInvitation函数中?但是当我这样做时,动作永远不会进入控制器。还是我必须添加处理提交操作的功能,$modal.open({ ...而不是引用控制器?虽然我更喜欢在自己的文件和范围中使用sendInvitationController。

谢谢你的帮助!

编辑

我们发现了几件有助于我们解决问题的方法,可能会帮助某人回答问题本身:

  1. $scope.invitation对象在中不是未定义的,sendInvitationController但是保存了正确的数据,而$scope.invitationForm仍未定义。
  2. 在send-invitation.html中,我们可以$scope.invitationForm.$invalid在此处访问并进行验证:<button type="button" ng-click="sendInvitation()" ng-disabled="invitationForm.$invalid">Invite</button>

所以问题是:为什么在表单模型正确绑定的同时将invitationForm对象绑定到$scope提交失败?


问题答案:

我有同样的问题,可以通过在模态控制器的范围内定义表单对象来解决。为了使您的代码正常工作,例如,将其放置$scope.form = {};在控制器的开头,并将form标签更改为<form name="form.invitation">。之后$scope.form.invitation.$invalid应填写。



 类似资料:
  • 我们有一个打开模态对话框的页面,表单如下。然而,当我们击中应该处理表单动作的控制器时,表单对象是未定义的,我是一个太多的Angular新手,无法理解为什么... 这是父页面控制器,具有打开模式对话框的功能: 在这样的页面上: “邀请”对话框html如下所示: 应该处理邀请的控制器在其他地方: 那么,将表单作用域放入控制器的正确方法是什么? 也许我需要将注入并向其添加功能?但是当我这样做的时候,动作

  • 本文向大家介绍详解AngularJS 模态对话框,包括了详解AngularJS 模态对话框的使用技巧和注意事项,需要的朋友参考一下 在涉及GUI程序开发的过程中,常常有模态对话框以及非模态对话框的概念 模态对话框:在子界面活动期间,父窗口是无法进行消息响应。独占用户输入 非模态对话框:各窗口之间不影响 主要区别:非模态对话框与APP共用消息循环,不会独占用户。 模态对话框独占用户输入,其他界面无法

  • 模态对话框(Modal Dialog)与非模态对话框(Modeless Dialog)的概念不是 Qt 所 独有的,在各种不同的平台下都存在。又有叫法是称为模式对话框,无模式对话框等。 所谓模态对话框就是在其没有被关闭之前,用户不能与同一个应用程序的其他窗口进 行交互,直到该对话框关闭。对于非模 态对话框,当被打开时,用户既可选择和该对话框进 行交互,也可以选择同应用程序的其他窗口交互。 在 Qt

  • 我有一个标题中描述的问题。 问题的小说明如下:我有一个按钮,用来打开对话框。然后,在该对话框中,有一个按钮,可以在第一个对话框的顶部打开另一个对话框。单击第二个按钮后,我希望调用控制器中的方法,但什么也没发生。h:outputText中的值被正确读取,所以我猜这不是连接控制器的问题- 我用的是: Spring web 3.1.2.发布 JSF 2.2.10 Primeface 5.1 代码: 豆。

  • 问题内容: 我正在尝试使用ui-bootstrap 0.6中的模式指令 这是ui-bootstrap页面上的默认工作插件: http://plnkr.co/edit/JGBiBSeRqOnwRhYA9py8?p=preview 现在,我试图使编码样式适合有角度的种子样式,以将其包含在一个应用程序中,如下所示: http://plnkr.co/edit/Y59rwlcNpQdijKtmjOPy?p=

  • 我正在尝试弹出一个自定义对话框,当我点击一个按钮,但它不会弹出在所有。我的应用程序基本上是一个日历,我将使用sqlite在日历中添加/保留约会和其他内容,使用对话框,这是指定约会细节的地方。 我为此使用的代码如下: 我做错了什么?