javascript – 为什么不调用AngularJS表单处理程序方法?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 为什么不调用AngularJS表单处理程序方法?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3445字,纯文字阅读大概需要5分钟。
内容图文
![javascript – 为什么不调用AngularJS表单处理程序方法?](/upload/InfoBanner/zyjiaocheng/773/baf9d07bb08846f39b0a50d890f92dca.jpg)
为什么下面显示的AngularJS表单不会在控制器中调用它的confForm()处理程序方法?我知道控制器连接到视图,因为视图打印在控制器中设置的confirmStatus和wleadid变量的值.但是,表单中的验证警告不会在用户的浏览器中打印,并且当用户单击提交按钮时没有任何操作.具体来说,下面的confForm()处理程序方法中有两个SYSO命令,它们永远不会打印到控制台,因此显示该方法未被触发.为了使confForm()表单处理程序方法运行,需要对下面的代码进行哪些具体更改?
这是视图html:
confirmStatus: {{ confirmStatus }}
<br>
webleadid: {{ wleadid }}
<div ng-show="confirmStatus=='success'">
<h1>Some title</h1>
<h2>Some message.</h2>
<form name="confirmForm" ng-submit="confForm(phoneForm.$valid)" novalidate>
Cell Phone number:
<input type="hidden" name="someData" ng-value="wleadid" />
<input type="text" name="phonenum1" ng-model="resultphone.phonenum1" required />
<input type="text" name="phonenum2" ng-model="resultphone.phonenum2" required />
<input type="text" name="phonenum3" ng-model="resultphone.phonenum3" required />
<p ng-show="phoneForm.phonenum1.$error.required && !phoneForm.phonenum1.$pristine" class="help-block">All 3 parts of cell phone number are required.</p>
<p ng-show="phoneForm.phonenum2.$error.required && !phoneForm.phonenum2.$pristine" class="help-block">All 3 parts of cell phone number are required.</p>
<p ng-show="phoneForm.phonenum3.$error.required && !phoneForm.phonenum3.$pristine" class="help-block">All 3 parts of cell phone number are required.</p>
<button type="submit" ng-disabled="phoneForm.$invalid" >Submit</button>
</form>
</div>
这里是JavaScript控制器,它能够将confirmStatus和webleadid变量填充到视图html,但是没有处理表单提交按钮点击:
angular.module('confirm', []).controller('confirm', function($scope, $http, $routeParams) {
// set the default value of our number
$scope.confirmStatus = "success";
$scope.wleadid = "30";
$scope.confForm = function(isValid) {
console.log("inside confForm")
if (isValid) {
console.log("confForm is Valid!")
var funcJSON = {type:"resultphone", wleadid: $scope.wleadid, phonenum1: $scope.phonenum1, phonenum2: $scope.phonenum2, phonenum3: $scope.phonenum3, };
console.log('form is valid. about to post... ')
$http.post('/submit-phone', funcJSON).then(function(response) {
$scope.confirmStatus = response.data.content;
});
}
};
});
解决方法:
问题是您正在验证您未提交的表单.在HTML中将’phoneForm’的实例更改为’confirmForm’,您应该得到您期望的行为.
confirmStatus: {{ confirmStatus }}
<br>
webleadid: {{ wleadid }}
<div ng-show="confirmStatus=='success'">
<h1>Some title</h1>
<h2>Some message.</h2>
<form name="confirmForm" ng-submit="confForm(confirmForm.$valid)" novalidate>
Cell Phone number:
<input type="hidden" name="someData" ng-value="wleadid" />
<input type="text" name="phonenum1" ng-model="resultphone.phonenum1" required />
<input type="text" name="phonenum2" ng-model="resultphone.phonenum2" required />
<input type="text" name="phonenum3" ng-model="resultphone.phonenum3" required />
<p ng-show="confirmForm.phonenum1.$error.required && !confirmForm.phonenum1.$pristine" class="help-block">All 3 parts of cell phone number are required.</p>
<p ng-show="confirmForm.phonenum2.$error.required && !confirmForm.phonenum2.$pristine" class="help-block">All 3 parts of cell phone number are required.</p>
<p ng-show="confirmForm.phonenum3.$error.required && !confirmForm.phonenum3.$pristine" class="help-block">All 3 parts of cell phone number are required.</p>
<button type="submit" ng-disabled="confirmForm.$invalid" >Submit</button>
</form>
</div>
内容总结
以上是互联网集市为您收集整理的javascript – 为什么不调用AngularJS表单处理程序方法?全部内容,希望文章能够帮你解决javascript – 为什么不调用AngularJS表单处理程序方法?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。