11

I am using Angularjs UI bootstrap to render Modal windows in my project. But In some situation I want to call a function after the modal loads. I have tried with $timeout and $viewContentLoaded but no use. can any one help me to resolve this issue.

Thank you all.

souser
  • 796
  • 2
  • 8
  • 25
chandu
  • 2,276
  • 3
  • 20
  • 35

2 Answers2

15

I go through the documentation of angular ui bootstrap and finally I found the solution.

The open method returns a modal instance, an object with the opened propertie:

opened - a promise that is resolved when a modal gets opened after downloading content's template and resolving all variables

to call function after model opens.

$modalInstance.opened.then(function(){
  alert('hi');
});
chandu
  • 2,276
  • 3
  • 20
  • 35
  • awesome, that was the problem I had before .. :D i was trying to do something before the promise was resolved .... good to know that you found the solution – Gabriel Matusevich Jun 23 '14 at 07:32
  • but here handle the opened method from $modelInstance in biggest issue. I am trying to call that method – chandu Jun 23 '14 at 07:38
  • 2
    the above method call the function on model loading – chandu Jun 23 '14 at 07:47
  • 9
    If you need to do DOM manipulation and you're waiting for the controller to be loaded *and* rendered, you might be looking for `$modalInstance.rendered.then(...`. – esqew Nov 24 '15 at 15:20
  • Can you give me a code snippet for the above solution ? urgent – Sanjay B Jan 11 '17 at 06:52
  • Just tried to summarize the useful methods that one may use: var modalInstance = $uibModal.open({ // your code }); modalInstance.opened.then(function(){ // your code }); modalInstance.rendered.then(function(){ // your code }); modalInstance.result.then(function(selectedItem) { // Returned from popup }, function() { $log.info('Modal dismissed at: ' + new Date()); }); – Victor Feb 14 '19 at 15:21
7

Alternatively, you can use:

$modalInstance.rendered.then(function(){
  alert('hi');
});

PS: this was originally pointed by @esqew user. I put it here just to be easier to find.

Daniel Loureiro
  • 4,595
  • 34
  • 48