Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trigger modal popup from controller

I have a bootstrap modal popup which I fill with data from my modalcontroller. After I filled it with data I would like to show it. To be able to show the modalpopup straight away when going into the page I would like to trigger it directly. Right now I have a button which does that, but how can I do it automatic in a proper way?

<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog" ng-init="getAllItems()">
    <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Modal Header</h4>
            </div>
            <div class="modal-body">
                <div class="">
                    <form class="form-horizontal" name="form" role="form">
                        <div ng-repeat="item in items">
                            <input type="radio" name="fundselector" ng-model="item" ng-value="item"/> {{item}} <br />
                        </div>
                    </form>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>

    </div>
</div>

My controller:

angular.module("Modal")
.controller("ModalController",
[
"$scope", "$rootScope", "$location", "ModalService", "AuthenticationService",
function ($scope, $rootScope, $location, ModalService, AuthenticationService) {

    AuthenticationService.GetCurrentWindowsUser(function(username) {
        $scope.username = username;
    });

    $scope.getAllItems = function () {
        AuthenticationService.GetCurrentWindowsUser(function (username) {
            if (username) {
                AuthenticationService.GetItems(username, function (items) {
                    $scope.items = items;
                    //Trigger modalpopup here
                });
            }
        });
    }
}
]);
like image 676
MrProgram Avatar asked Oct 19 '25 12:10

MrProgram


2 Answers

Don't use jquery, use angular-ui.

https://angular-ui.github.io/bootstrap/#/modal

You can open the modal programmatically like below, and pass your data in in the resolve. This is copy and pasted straight from their example.

var modalInstance = $uibModal.open({
      animation: $scope.animationsEnabled,
      templateUrl: 'myModalContent.html',
      controller: 'ModalInstanceCtrl',
      size: size,
      resolve: {
        items: function () {
          return $scope.items;
        }
      }
    });
like image 112
mgiesa Avatar answered Oct 21 '25 04:10

mgiesa


Find element by angular.element and hide.

var popup = angular.element("#modal-default");
//for hide model
popup.modal('hide');
//for show model
popup.modal('show');
like image 44
vicky Avatar answered Oct 21 '25 04:10

vicky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!