0

I'm trying to submit a form when user clicks on any part of the form, and then process it using AnglujarJS. Here's how I tried doing it:

<form ng-click="submit()" ng-app="MyApp" ng-controller="MyCtr">
      <input type="text" ng-model="my_val" name="my_val" value="0" style="display: none"/>
</form>

var app = angular.module('MyApp', []);
app.controller('MyCtr', function($scope) {
    $scope.submit = function() {
            $scope.my_val; // This is undefined
        });
    };
});

The problem is that $scope does not have form values. If I replace ng-click with ng-submit, the values are present, but I don't want to submit form by clicking on a submit button.

2
  • You need to declare $scope.my_val outside of the submit function. Commented Oct 22, 2017 at 23:19
  • To publish the form instance into scope, you need to specify the name attribute. <form name="example"> and then you can use $scope.example.my_val
    – alexhuang
    Commented Oct 23, 2017 at 0:05

1 Answer 1

3

You need to declare and set to 0 $scope.my_valoutside of submit function and also ij Js code there are unnecessary closed brackets check that as well.

var app = angular.module('MyApp', []);
app.controller('MyCtr', function($scope) {
$scope.my_val="0";
  $scope.submit = function() {
    $scope.fromvalue.my_val
    console.log($scope.fromvalue.my_val.$viewValue);
  };

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<form name="fromvalue" ng-click="submit()" ng-app="MyApp" ng-controller="MyCtr">
<input type="text" ng-model="my_val" name="my_val" value="0"/>
</form>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.