Search code examples
angularjsformssubmitbootstrap-modalangular-bootstrap

AngularJS & Bootstrap modal Form. Why data is getting submitted on cancel


I face an issue with AngularJS and Bootstrap UI in a modal Form where on cancel the form is triggering the submission. Here is my Plunker to demonstrate my problem. On cancel I get an Alert inside submission which should not happen. What is wrong with this snippet?

Markup

     <div ng-controller="modalForm">
        <script type="text/ng-template" id="myModalContent.html">

                <div id="messages" class="well" ng-show="message">{{ message }}</div>
                <form name="addDomainForm" novalidate ng-submit="submit(addDomainForm)">
                <div class="modal-header">
                    <h6 class="modal-title"><i class="fa fa-plus"></i> add new Item </h6>
                </div>

                <div class="modal-body">

                        <div class="form-group">
                            <input type="domain" ng-model="formData.domain" class="form-control" id="domainAddress" placeholder="Domain Adresse">
                        </div>

                        <textarea rows="10" ng-model="formData.description" class="form-control" placeholder="Beschreiben Sie Ihre Aktivität" ng-minlength="15"></textarea>

                </div>

                <div class="modal-footer">
                    <input type="submit" class="btn btn-primary" value="Save" />
                    <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
                </div>
                </form>


            </script>

            <button class="btn btn-success" ng-click="open()"><i class="glyphicon glyphicon glyphicon-plus-sign icon-plus-sign"></i> new Item </button>

</div>

Angular

angular.module("testModal", ['ui.bootstrap'])
.controller("modalForm", function($scope, $modal) {
  $scope.addDomainForm = {};

  $scope.open = function (size) {
    var modalInstance = $modal.open({
      templateUrl: 'myModalContent.html',
      controller: ModalInstanceCtrl,
      size: size
    });

  };

}); 

var ModalInstanceCtrl = function ($scope, $modalInstance,$log) {

  $scope.cancel = function () {
    $modalInstance.dismiss('cancel');
  };

  $scope.submit = function(addDomainForm) {
   alert();
  };

};

Solution

  • Adding type attribute; type="button" to the button works for me:

    Example;

     <button type="button" class="btn btn-warning" ng-click="authModel.cancel()">Cancel</button>`