简体   繁体   English

AngularJS Bootstrap Modal $ modalInstance.dismiss不是函数

[英]AngularJS Bootstrap Modal $modalInstance.dismiss is not a function

When I click the cancel button on my modal, the $modalInstance.dismiss function binded with ng-click on my modal template isn't working. 当我单击模态上的“ 取消”按钮时,与ng-click绑定的模态模板绑定的$ modalInstance.dismiss函数不起作用。

The console has been throwing the error: "$modalInstance.dismiss is not a function" 控制台已抛出错误:“ $ modalInstance.dismiss不是函数”

MODAL TEMPLATE: 模态模板:

<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>

MAIN 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]);
})();

MODAL 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]);
})();

I've spent about 3 hours messing around with my code, looking at the AngularJS Bootstrap UI documentation, several StackOverflow threads, and other sites and have gotten no where. 我花了大约3个小时来弄乱我的代码,查看AngularJS Bootstrap UI文档,几个StackOverflow线程和其他站点,却一无所获。 Any help would be appreciated. 任何帮助,将不胜感激。

In the version of angular ui bootstrap you're using, the reference to the modal instance is called $uibModalInstance . 在您使用的ui ui引导程序版本中,对模式实例的引用称为$uibModalInstance So try changing your controller to this: 因此,尝试将控制器更改为此:

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

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

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