Angularjs Form Submit Example – onlinecode

Angularjs Form Submit Example – onlinecode

In this post we will give you information about Angularjs Form Submit Example – onlinecode. Hear we will give you detail about Angularjs Form Submit Example – onlinecodeAnd how to use it also give you demo for it if it is necessary.

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>
<script>  angular.module('mydemoapp', [])    .controller('DemoController', ['$scope', function($scope) {      $scope.list = [];      $scope.text = 'Welcome to Expert PHP';      $scope.doSomeAction = function() {        if ($scope.text) {          $scope.list.push(this.text);          $scope.text = '';        }      };    }]);</script><form ng-submit="doSomeAction()" ng-controller="DemoController">  Enter text and hit enter:  <input type="text" ng-model="text" name="text" />  <input type="submit" id="submit" value="Submit" />  <pre>list={{list}}</pre></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>
<script>  angular.module('mydemoapp', [])    .controller('DemoController', ['$scope', function($scope) {      $scope.list = [];      $scope.text = 'Welcome to Expert PHP';      $scope.doClickAction = function() {        if ($scope.text) {          $scope.list.push(this.text);          $scope.text = '';        }      };    }]);</script><form  ng-controller="DemoController">  Enter text and hit enter:  <input type="text" ng-model="text" name="text" />  <input type="button" id="button" value="Submit" ng-click="doClickAction()" />  <pre>list={{list}}</pre></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>
<script>  angular.module('mydemoapp', [])    .controller('DemoController', ['$scope', function($scope) {      $scope.list = [];      $scope.text = 'Welcome to Expert PHP';      $scope.doSomeAction = function(event) {        event.preventDefault();        if ($scope.text) {          $scope.list.push(this.text);          $scope.text = '';        }      };    }]);</script><form action="page.php" ng-submit="doSomeAction()" ng-controller="DemoController">  Enter text and hit enter:  <input type="text" ng-model="text" name="text" />  <input type="submit" id="submit" value="Submit" />  <pre>list={{list}}</pre></form>
Now you can submit form in angularjs in different ways.

Label :

Angular JS

Web Development

Hope this code and post will helped you for implement Angularjs Form Submit Example – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  83  =  91

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US