我尝试在用户点击表单的任何部分时提交表单,然后使用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
,则会显示值,但我不想通过单击提交按钮来提交表单。
答案 0 :(得分:3)
你需要在$scope.my_val
函数之外声明并设置为0 submit
,并且ij Js代码也有不必要的闭括号检查。
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;