gpt4 book ai didi

javascript - AngularJS Bootstrap Modal $modalInstance.dismiss 不是一个函数

转载 作者:行者123 更新时间:2023-12-03 02:57:19 24 4
gpt4 key购买 nike

当我单击模态上的取消按钮时,与模态模板上的 ng-click 绑定(bind)的 $modalInstance.dismiss 函数不起作用。

控制台抛出错误:“$modalInstance.dismiss 不是函数”

模态模板:

<div class="my-modal ng-scope" id="my-modal">
<div class="modal-header">
<h3 class="modal-title" id="modal-title">Create a new room</h3>
</div>
<div class="modal-body" id="modal-body">
<form>
Enter a room name<br>
<input type="text" name="new-room-name">
</form>
<div class="modal-footer">
<button class="btn btn-warning" type="button" ng-click="modal.cancel()">Cancel</button>
<button class="btn btn-primary" type="button" ng-click="modal.save()">Create Room</button>
</div>
</div>

主 Controller :

(function() {
function HomeCtrl(Room, $scope, $uibModal, $log, $document) {
var home = this;

home.chatRooms = Room.all;
//TO TEST ADD METHOD FROM ROOM.JS
// this.addRoom = Room.add();

home.open = function () {
modalInstance = $uibModal.open({
animation: true,
backdrop: true,
templateUrl: '../templates/modal.html',
controller: 'ModalInstanceCtrl',
controllerAs: 'modal',
bindToContoller: true,
scope: $scope,
size: 'lg',
resolve: {
'$modalInstance': function () { return function () { return modalInstance; } }
}
});


console.log(modalInstance);

modalInstance.result.then(function (newChatRoom) {
home.selected = newChatRoom;
console.log(newChatRoom);
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
}

angular
.module('blocChat')
controller('HomeCtrl', ['Room', '$scope', '$uibModal', '$log', '$document', HomeCtrl]);
})();

模态 Controller :

(function() {
function ModalInstanceCtrl(Room, $scope, $modalInstance, $log, $document) {
var modal = this;

this.save = function() {
$modalInstance.close(newChatRoom);
};

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

}

angular
.module('blocChat')
.controller('ModalInstanceCtrl', ['Room', '$scope', '$modalInstance', '$log', '$document', ModalInstanceCtrl]);
})();

我花了大约 3 个小时摆弄我的代码,查看 AngularJS Bootstrap UI 文档、几个 StackOverflow 线程和其他网站,但一无所获。任何帮助将不胜感激。

最佳答案

在您使用的 Angular ui Bootstrap 版本中,对模态实例的引用称为 $uibModalInstance。因此,请尝试将您的 Controller 更改为:

(function() {
function ModalInstanceCtrl(Room, $scope, $uibModalInstance, $log, $document)
{
var modal = this;

this.save = function() {
$uibModalInstance.close(newChatRoom);
};

this.cancel = function() {
$uibModalInstance.dismiss('cancel');
};
}

angular
.module('blocChat')
.controller('ModalInstanceCtrl', ['Room', '$scope', '$uibModalInstance', '$log', '$document', ModalInstanceCtrl]);
})();

关于javascript - AngularJS Bootstrap Modal $modalInstance.dismiss 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47567484/

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