Petra
Petra

Reputation: 744

Is there a way to trigger .opened() in angular-ui modal?

I'm trying to find out how to trigger .opened() for my modal. What I want to happen is, after all the data is loaded in my modal, I would like to .opened() to be triggered. I am aware that opened() is triggered when the modal is already opened, is there a way to change that, like maybe only trigger it after I have initialized the values in my controller?

    var modalInstance = $modal.open({
      templateUrl: 'myModalContent.html',
      controller: 'ModalInstanceCtrl'
    });

    modalInstance.opened.finally(function() {
           doSomething();
    });

asdas

angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($scope, $modalInstance, items) {

  init();

  function init() {
     // do initializations here....
     // then resolve the opened() function... but how?
  }

  $scope.ok = function () {
    $modalInstance.close($scope.selected.item);
  };

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

Upvotes: 1

Views: 1760

Answers (1)

Petr Averyanov
Petr Averyanov

Reputation: 9486

Just pass object with 'onOpen' function:

  $scope.ctrl = {
    onOpen: function() {
      console.log('open!');
    }
  };

    var modalInstance = $uibModal.open({
      animation: $scope.animationsEnabled,
      templateUrl: 'myModalContent.html',
      controller: 'ModalInstanceCtrl',
      size: size,
      resolve: {
        ctrl: function () {
          return $scope.ctrl;
        }
      }
    });

and then:

function init() {
  ctrl.onOpen();
}

http://plnkr.co/edit/wxwkV1cjT1gO8xGZDVgm?p=preview

Upvotes: 2

Related Questions