简体   繁体   English

在AngularJS UI中关闭模态

[英]Closing A Modal in AngularJS UI

Quite new with AngularJS (and AngularJS UI) and I am unable to close a modal window. AngularJS(和AngularJS UI)相当新,我无法关闭模态窗口。

The HTML code as follows: 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>

Controller: 控制器:

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

        });

Pretty much, after making the POST request, I want to close the modal window, but I don't have any idea how. 几乎,在发出POST请求后,我想关闭模态窗口,但我不知道如何。 Not sure how I can reference the modal window which I opened. 不知道我怎么能引用我打开的模态窗口。

Any insight appreciated. 有任何见解赞赏。

Update: My modal's html includes an Save and Cancel button. 更新:我的模态的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>

The modal does close when I hit the Cancel button. 当我点击取消按钮时,模态会关闭。 What I want to achieve is being able to close the modal when the addAgency function is completed. 我想要实现的是能够在addAgency函数完成时关闭模态。

You need to save your modal instance in the $scope so that you have a reference to it later. 您需要将模式实例保存在$ scope中,以便稍后引用它。

So you'd init your $scope.modalInstance = null; 所以你要初始化$scope.modalInstance = null; at the top. 在顶部。

Then you'd open your modal like this: 然后你就像这样打开你的模态:

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

To close, you would then call $scope.modalInstance.close(); 要关闭,您将调用$scope.modalInstance.close(); (which would go where you have your // HOW TO CLOSE THE MODAL FROM HERE? WHAT FUNCTION DO I CALL? comment. (这将是你的地方// HOW TO CLOSE THE MODAL FROM HERE? WHAT FUNCTION DO I CALL?评论。

Here's a plunkr that shows how to do it: EXAMPLE 这是一个展示如何做到的傻瓜: 例子

To close your modal you have 2 functions : "close" and "dismiss". 要关闭你的模态,你有两个功能:“关闭”和“关闭”。

Let say that the end of your modal html file looks like that : 假设您的模态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>

All you have to write in your modal controller is that : 您必须在模态控制器中编写的是:

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

And if you want to know if the user clicked on "cancel" or "ok" you have to change your function "showModalAddAgency" like that : 如果您想知道用户是否点击了“取消”或“确定”,您必须更改您的“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
    });
};

I hope my answer fit you. 我希望我的回答适合你。

Have a nice day ! 祝你今天愉快 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM