Angularjs Form Submit Example

Angularjs Form Submit Example

Angularjs Form Submit Example in Different Ways

As we know there are so many option to do any task, in same way using angularjs we can submit form in many ways.

What will we do :

We will create a view with attribute ng-controller in which we create form with single input text field when form is submitted then we will handle the form request in angularjs.

Step1: Form Submit using ng-submit directive

ng-submit handler is fired on form submission. This is a popular way to submit form in angularjs

You can define ng-submit as element and also it can be define as attribute.


  1. <script>
  2. angular.module('mydemoapp', [])
  3. .controller('DemoController', ['$scope', function($scope) {
  4. $scope.list = [];
  5. $scope.text = 'Welcome to Expert PHP';
  6. $scope.doSomeAction = function() {
  7. if ($scope.text) {
  8. $scope.list.push(this.text);
  9. $scope.text = '';
  10. }
  11. };
  12. }]);
  13. </script>
  14. <form ng-submit="doSomeAction()" ng-controller="DemoController">
  15. Enter text and hit enter:
  16. <input type="text" ng-model="text" name="text" />
  17. <input type="submit" id="submit" value="Submit" />
  18. <pre>list={{list}}</pre>
  19. </form>
Step1: Form Submit without ng-submit directive

If you don't want to use ng-submit and form should be submitted then use ng-click event handler.

ng-click is triggered when any element is clicked.

You can define ng-click as element and also it can be define as attribute.


  1. <script>
  2. angular.module('mydemoapp', [])
  3. .controller('DemoController', ['$scope', function($scope) {
  4. $scope.list = [];
  5. $scope.text = 'Welcome to Expert PHP';
  6. $scope.doClickAction = function() {
  7. if ($scope.text) {
  8. $scope.list.push(this.text);
  9. $scope.text = '';
  10. }
  11. };
  12. }]);
  13. </script>
  14. <form ng-controller="DemoController">
  15. Enter text and hit enter:
  16. <input type="text" ng-model="text" name="text" />
  17. <input type="button" id="button" value="Submit" ng-click="doClickAction()" />
  18. <pre>list={{list}}</pre>
  19. </form>
Step3: Submit Form using both ng-submit and action attribute

If you are using Laravel to generate form then Laravel blade generate form action attribute and in that case angularjs won't process due to action attribute.In that scenario you can use $event handler to stop the default action.


  1. <script>
  2. angular.module('mydemoapp', [])
  3. .controller('DemoController', ['$scope', function($scope) {
  4. $scope.list = [];
  5. $scope.text = 'Welcome to Expert PHP';
  6. $scope.doSomeAction = function(event) {
  7. event.preventDefault();
  8. if ($scope.text) {
  9. $scope.list.push(this.text);
  10. $scope.text = '';
  11. }
  12. };
  13. }]);
  14. </script>
  15. <form action="page.php" ng-submit="doSomeAction()" ng-controller="DemoController">
  16. Enter text and hit enter:
  17. <input type="text" ng-model="text" name="text" />
  18. <input type="submit" id="submit" value="Submit" />
  19. <pre>list={{list}}</pre>
  20. </form>

Phone: (+91) 8800417876
Noida, 201301