ot954
ot954

Reputation: 435

Trigger ng-submit from directive

This type of similar question has been asked and I followed all the suggestions still I am not able to trigger submit from directive. I have multiple form elements for multi step form validation and I have one click button to validate all forms. Here is the html.

<form name="employeePersonalForm" ext-submit
      ng-submit="emp.submittedForm(employeePersonalForm)" novalidate>
   <div layout="row">
       <div class="default-form-item pr-3" ng-class="{'md-input-invalid': employeePersonalForm.$submitted && employeePersonalForm.firstName.$invalid}" flex>
           <label class="default-label required">First Name {{employeePersonalForm.firstName.$invalid}} - {{employeePersonalForm.$submitted}}</label>
            <div class="default-input-wrapper">
                <input ng-model="emp.firstName" class="default-input" name="firstName" type="text" required>
                <p class="default-input-error">This field is required</p>
            </div>
       </div>
       <div class="default-form-item pl-3" flex>
           <label class="default-label required">Last Name</label>
           <div class="default-input-wrapper">
               <input ng-model="emp.lastName" class="default-input" type="text" required>
                <p class="default-input-error">This field is required</p>
           </div>
       </div>
    </div>
</form>

This type of multiple form exists in page. And there is one external button as follows.

<button class="default-btn default-btn--green" type="button" ng-click="nextStep($event)">Next</button>

On clicking of Next button, I am broadcasting event to submit the form. Here is the controller part.

emp.submittedForm = function(form) {
    if($scope.step==1) {
        alert(form.employeePersonalForm.$valid ? 'Valid' : 'Invalid!!!');
    }
}
$scope.nextStep = function() {
    if($scope.step == 1) {
        $rootScope.$broadcast('makeSubmit', {formName: 'employeePersonalForm'})
    }
};

I am catching this event in directive ext-submit as follows

(function () {
'use strict';

angular
    .module('app')
    .directive('extSubmit', ['$timeout', externalSubmit]);
/* @ngInject */
function externalSubmit($timeout) {
    return {
        link: function(scope, el, attr) {
            scope.$on('makeSubmit', function(event, data){
              if(data.formName === attr.name) {
                $timeout(function() {
                    el.triggerHandler('submit');
                }, 0, false);   
              }
            })
        }
    };
  }
})();

But it is not triggering ng-submit. I have followed this suggestion this link

Upvotes: 1

Views: 160

Answers (1)

georgeawg
georgeawg

Reputation: 48968

One approach is to simply execute the function in the ng-submit attribute:

app.directive('extSubmit', ['$timeout', externalSubmit]);
/* @ngInject */
function externalSubmit($timeout) {
    return {
        require: "form",
        link: function(scope, elem, attrs, form) {
            scope.$on('makeSubmit', function(event, data){
              if(data.formName === attrs.name) {
                //SET submitted state
                form.$setSubmitted();
                //EVALUATE ng-submit
                scope.$eval(attr.ngSubmit);
                //$timeout(function() {
                //    elem.triggerHandler('submit');
                //}, 0, false);   
              }
            })
        }
    };
  }
})

For more information, see

Upvotes: 1

Related Questions