Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I submit a form when input ng-change in AngularJS

What is the "AngularJS way" of doing a form submit when any of its inputs have been clicked (or changed)?

<form ng-submit="submit($event)" id="myForm">
  <input type="checkbox" name="foo" value="bar" ng-click="???"/>
</form>

I'm tempted to use jQuery and simply doing ng-click="$('#myForm').submit()", but it's probably worth learning it properly.

I have tried doing ng-click="submit($event)", but the error here is the $event object within the scope of the input instead of the entire form (correct me if I'm wrong, this is what I'm getting from the documentation).

like image 719
Helen Che Avatar asked Dec 21 '14 09:12

Helen Che


2 Answers

Well, you can do something like this for sure by triggering the AngularJS submit event:

$scope.change = function($event) {
    $timeout(function() {
        angular.element($event.target.form).triggerHandler('submit');
    });
};

where

<input type="checkbox" name="foo" value="bar" ng-click="change($event)" />

However I think it's better to simply use the same function in ngClick as used in ngSubmit.

Demo: http://plnkr.co/edit/tJIYD9ZVjYzwA2aXJobo?p=preview

like image 56
dfsq Avatar answered Oct 25 '22 04:10

dfsq


ng-change is what worked for me using a text input:

<form>
    <select data-ng-model="headerName" data-ng-options="header for header in headers"
            data-ng-change="calculateCorrelations()"></select>
</form>
like image 22
thebiggestlebowski Avatar answered Oct 25 '22 03:10

thebiggestlebowski