Agen-6ren">
gpt4 book ai didi

angularjs - 在 AngularJS UI 中关闭模态

转载 作者:行者123 更新时间:2023-12-01 00:58:13 24 4
gpt4 key购买 nike

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>

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

});

几乎,在发出 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 功能完成时关闭模式。

最佳答案

您需要将模态实例保存在 $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

关于angularjs - 在 AngularJS UI 中关闭模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25857347/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com