0

close is not a function在尝试关闭由$uibModal.open()

我已将问题减少到最少的代码:

    var app = angular.module('demoApp', ['ui.bootstrap']);

    app.controller('DemoContrller', function($scope, $uibModal) {
      var vm = this;
      var myModalInstance = null;

      vm.openModal = function() {

        myModalInstance = $uibModal.open({
                  animation: true,
                  templateUrl: 'modalTemplate.html',
                  scope: $scope // important - want to reuse current scope and controller and not create new
              }).result.then(function() {
                  console.log('fully closed with successful validation');
              });

      };


      vm.onApplyModal = function() {

          console.log('doing some validation and closing only if succeeded');

          // ??? close is not a function, myModalInstance is a Promise

          console.log(myModalInstance);
          myModalInstance.close();
      };

    })

请在此处查看我的 Plunk:Cannot close uibmodal from a function in the same scope

4

1 回答 1

3
 myModalInstance = $uibModal.open({
                  animation: true,
                  templateUrl: 'modalTemplate.html',
                  scope: $scope // important - want to reuse current scope and controller and not create new
              }).result.then(function() {
                  console.log('fully closed with successful validation');
              });

首先分配变量,然后链:

 myModalInstance = $uibModal.open({
                  animation: true,
                  templateUrl: 'modalTemplate.html',
                  scope: $scope // important - want to reuse current scope and controller and not create new
              });
myModalInstance .result.then(function() {
                  console.log('fully closed with successful validation');
              });
于 2017-12-04T11:08:28.107 回答