【问题标题】:Closing A Modal in AngularJS UI在 AngularJS UI 中关闭模态
【发布时间】:2023-06-04 10:41:01
【问题描述】:

AngularJS(和 AngularJS UI)相当新,我无法关闭模式窗口。

HTML代码如下:

<div>
    <div data-ng-show="agencies || agencies.length > 0">
        <div>
            <h3>
                Agencies
            </h3>
        </div>
        <div>
            <a href="" data-ng-click="showModalAddAgency()"><span class="glyphicon glyphicon-cloud-upload"></span>&nbsp;Add</a>
        </div>
    </div>
</div>

控制器:

    'use strict';

        app.controller('agencyController', function ($scope, $modal, agenciesDataService) {
            $scope.agencies = [];
            $scope.agency = null;
            init();

            function init() {
                getAgencies();
            };

        function getAgencies() {
                var onResponse = function (results) {
                    $scope.agencies = results.data;
                };

                var onError = function (error) {
                    alert(error.message);
                };

                agenciesDataService.getAgencies()
                    .then(onResponse, onError);
            };

        $scope.showModalAddAgency = function () {
                var modalInstance = $modal.open({
                    templateUrl: 'app/views/agencydetail.html',
                    controller: 'agencyController',
                    backdrop: 'static'
                });
            };

        $scope.addAgency = function addAgency() {
                var currentAgency = this.agency;

                var onResponse = function (results) {
                    currentAgency.Id = results.data.Id;
                    currentAgency.Name = results.data.Name;
                    currentAgency.AgencyPortalAccountId = results.data.AgencyPortalAccountId;
                    $scope.agencies.push(currentAgency);
                    currentAgency = {};

                    // HOW TO CLOSE THE MODAL FROM HERE? WHAT FUNCTION DO I CALL?
                };

                var onError = function (error) {
                    //alert(error.message);
                }

                agenciesDataService.addAgency(currentAgency)
                    .then(onResponse, onError);
            };

        });

差不多,在发出 POST 请求后,我想关闭模式窗口,但我不知道如何。不知道如何引用我打开的模式窗口。

任何见解都值得赞赏。

更新: 我的模态的 html 包括一个保存和取消按钮。

<div class="modal-footer">
<button class="btn btn-primary normal-button"
            ng-click="addAgency()">Save</button>
    <button class="btn btn-warning" ng-click="$dismiss()" style="width:100px;">Cancel</button>
</div>

当我点击取消按钮时,模式确实关闭了。我想要实现的是能够在 addAgency 功能完成时关闭模态。

【问题讨论】:

  • 查看文档中示例中显示的关闭和关闭方法

标签: angularjs angular-ui-bootstrap


【解决方案1】:

您需要将模态实例保存在 $scope 中,以便以后可以引用它。

所以你会在顶部初始化你的$scope.modalInstance = null;

然后你会像这样打开你的模式:

$scope.modalInstance = $modal.open({
    templateUrl: 'app/views/agencydetail.html',
    controller: 'agencyController',
    backdrop: 'static'
});

要关闭,您可以调用 $scope.modalInstance.close();(这将转到您有 // HOW TO CLOSE THE MODAL FROM HERE? WHAT FUNCTION DO I CALL? 评论的地方。

这里有一个 plunkr 展示了如何做到这一点:EXAMPLE

【讨论】:

  • 如果我按照您的建议编写代码,会发生应用程序挂起的情况。 (Chrome Kill Pages 对话框弹出)。我做了 $scope.modalInstance = function(){ $modal.open({});};它确实打开了模式,但仍然发出 $scope.modalInstance.close();抛出未定义的不是函数错误。
  • 试试 $scope.modalInstance.$close()
  • 另外,试试 '$dismiss()' $scope.modalInstance.$dismiss()
  • 我添加了一个有效的示例 plunkr - 试试看。
  • 我找到了问题所在,部分感谢您提出的解决方案。
【解决方案2】:

要关闭您的模式,您有 2 个功能:“关闭”和“关闭”。

假设你的模态 html 文件的结尾是这样的:

<div class="modal-footer">
    <input type="button" class="submit" value="Save" ng-click="ok()" />
    <input type="button" class="cancel" value="Cancel" ng-click="cancel()" />
</div>

所有你必须在你的模态控制器中写的是:

$scope.cancel = function(){
    $modalInstance.dismiss('cancel');
};
$scope.ok = function(){
    // you can pass anything you want value object or reference object
    $modalInstance.close("clicked ok"); 
};

如果您想知道用户是否点击了“取消”或“确定”,您必须像这样更改您的函数“showModalAddAgency”:

$scope.showModalAddAgency = function () {
    var modalInstance = $modal.open({
         templateUrl: 'app/views/agencydetail.html',
         controller: 'agencyController',
         backdrop: 'static'
    });

    modalInstance.result.then(function (resultOk) {
         // OK
    }, function (resultCancel) {
         // CANCEL
    });
};

希望我的回答适合你。

祝你有美好的一天!

【讨论】:

  • 在我的模式中,当用户单击“确定”按钮时,会执行一些额外的过程(因此,在我原来的问题中,我询问要在我的 $scope.addAgency 函数中添加什么代码) .
  • 一种解决方案是必须在您的模态中使用基本功能“ok”和“cancel”。如果用户单击“取消”,则您“解雇”。如果用户单击“确定”,您将关闭。在“showModalAddAgency”中,如果用户单击“ok”,您将调用 AddAgency。
  • 但在知道 addAgency 调用成功之前,我不想直接关闭模式......
  • 请试试这个:在你的控制器上注入服务“$modalInstance”。在 AddAgency 中,您在其中发表评论:“// HOW TO CLOSE THE MODAL FROM HERE?WHAT FUNCTION DO I CALL?”,将其替换为:“$modalInstance.close();” 如果用户单击取消,则模态关闭如果用户单击 addAgency,则仅当请求成功时模态才会关闭。
  • 出现错误:ReferenceError: $modalInstance is not defined