1

Whilst I have looked at the answers here, I'm still unable to trigger my openModal() function on page load.

Within my controller, I have the code (below question), which does allow openModal() to trigger fine either from an ng-click directive or when entering the following in the Chrome console:

angular.element($0).scope().openModal()

My problem is when I'm trying to conditionally open the modal upon page load. My current code is:

$scope.openModal = function (size) {

    modalInstance = $modal.open({
        templateUrl: 'myModalContent.html',
        controller: 'pageBuilder',
        size: 'lg',
        resolve: {
            items: function () {
                return $scope.chosenDeploymentSchedule;
            }
        }
    });
};

angular.element(document).ready(function () {

//  $scope.openModal();
    console.log('Hello World');
});

I do get my Hello World printing, but when I try uncommenting the $scope.openModal();, I get an infinite loop of undefined + Hello World printing out.

Community
  • 1
  • 1
ljs.dev
  • 4,449
  • 3
  • 47
  • 80

3 Answers3

2

I don't know if this solution is too late for this question but may be useful for someone.

  1. Create your normal bootstrap modal

    $scope.myDialog = function () {
        $uibModal.open({
           templateUrl: 'templatefolder/modal.html',
           backdrop: 'static',
           windowClass: 'modal',
           size: 'lg',
           controller: function ($scope, $uibModalInstance) {
                $scope.cancel = function () {
                   $uibModalInstance.dismiss();
                };
              }
        });
    };  
    
  2. Call the modal in your html page like so <span ng-init="myDialog()"></span>

That's it. This works for me and I hope it will work for someone too.

aurel
  • 54
  • 1
  • 11
Kwesi Aryee
  • 310
  • 2
  • 15
0

You can call $scope.openModal() directly underneath where you define it in the controller. It'll open the modal right away.

nairys
  • 333
  • 3
  • 9
0

Whilst still unable to call it directly from within the same controller, my solution was to add another method which gets called from an ng-init in the view:

$scope.launchModalConditionally = function(){
    if(!$scope.gotCredentials){
        $scope.openModal();
    }
};
ljs.dev
  • 4,449
  • 3
  • 47
  • 80