AngularJS Bootstrap模态$ modalInstance.dismiss不是函数

时间:2017-11-30 06:50:53

标签: javascript angularjs angular-ui-bootstrap modalviewcontroller angular-ui-modal

当我点击模态上的取消按钮时,在模态模板上使用ng-click绑定的$ 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>

主控制器:

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

我花了大约3个小时搞乱我的代码,查看AngularJS Bootstrap UI文档,几个StackOverflow线程和其他网站,并且没有在哪里。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

在您正在使用的angular ui bootstrap版本中,对模态实例的引用称为$uibModalInstance。因此,请尝试将控制器更改为:

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