单击表单上的AngularJS流程表单

时间:2017-10-22 23:13:19

标签: javascript jquery html angularjs forms

我尝试在用户点击表单的任何部分时提交表单,然后使用AnglujarJS处理它。以下是我尝试的方法:

<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
        });
    };
});

问题是$scope没有表单值。如果我将ng-click替换为ng-submit,则会显示值,但我不想通过单击提交按钮来提交表单。

1 个答案:

答案 0 :(得分:3)

你需要在$scope.my_val函数之外声明并设置为0 submit,并且ij Js代码也有不必要的闭括号检查。

&#13;
&#13;
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);
  };

});
&#13;
<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>
&#13;
&#13;
&#13;