0

angular-ui-modal]1 for create my site and I want to show my modal automatically when the user enter in my webpage. With ng-click is easy , I have this code:

var app = angular.module('app', ['ui.bootstrap']);
app.controller('HomeCtrl', ['$scope', '$modal', function($scope, $modal) {
$scope.openModal = function(data) {
  var modalInstance = $modal.open({
    templateUrl: 'modals/register.html',
    resolve: {
      data: function() {
        return data === null ? {} : data;
      }
    }
  });
};
}]);

But I do not know how trigger the modal when load page. Any help? Please

2
  • Just call the same code outside of the scope function. Also $modal is an old version so you may want to consider upgrading. Commented Jan 16, 2017 at 16:10
  • Possible duplicate of Bootstrap-angular-ui modal on load Commented Jan 16, 2017 at 17:41

1 Answer 1

1

You just have to call the function when the controller is loaded:

var app = angular.module('app', ['ui.bootstrap']);
app.controller('HomeCtrl', ['$scope', '$modal', function($scope, $modal) {
  $scope.openModal = function(data) {
    $scope.blurred  = "blurredopen";
    var modalInstance = $modal.open({
      templateUrl: 'modals/register.html',
      resolve: {
        data: function() {
          return data === null ? {} : data;
        }
      }
    });
  };

  $scope.openModal(); // <-- Call it
}]);

Demo on JSFiddle

Sign up to request clarification or add additional context in comments.

5 Comments

Added a JSFiddle demo. Be sure you call $scope.openModal() after declaring the function.
other question @Mistalis , if i want the modal appears after x seconds? Thanks for your help
use $timeout, if you've found Mistalis's answer helpful then please accept or at least upvote it.
Thanks @svarog, how i must use $timeout?
yes works fine with timeout but i have a hard problem, cant add a contoller to modal

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.