0
votes

I'm new to AngularJS, and were just going through Angular UI Modal described in: http://angular-ui.github.io/bootstrap/.

I want to create a simple modal that have just only one button to close the modal. How to do this without create a separate controller for the modal? Is there a script for ng-click event in the modal template to do the job? Such as this.close()...

What I want to achieve look like this:

Template:

<div class="modal-header">
  <h3>Modal header</h3>
</div>
<div class="modal-body">
 <h4>Just something random here</h4>
</div>
<div class="modal-footer">
    <button class="btn btn-primary" ng-click="this.close()">OK</button>
</div>

Page controller:

  $scope.openModal = function() {
    $uibModal.open({
      templateUrl: "modalContent.html",
      // it's so simple so that I don't want a separate controller
      //controller: "ModalContentCtrl",
      size: '',
      backdrop: 'static',
      keyboard: false
    });
  };
1
controller can be a function also - charlietfl

1 Answers

2
votes

From the docs:

The scope associated with modal's content is augmented with:

$close(result) (Type: function) - A method that can be used to close a modal, passing a result.

$dismiss(reason) (Type: function) - A method that can be used to dismiss a modal, passing a reason.

Those methods make it easy to close a modal window without a need to create a dedicated controller.

So this works:

<div class="modal-header">
  <h3>Modal header</h3>
</div>
<div class="modal-body">
 <h4>Just something random here</h4>
</div>
<div class="modal-footer">
    <button class="btn btn-primary" ng-click="$close()">OK</button>
</div>