[英]Angular 1.3 : TypeError: Cannot read property '0' of null error on ng-submit
I have a simple form that I am trying to submit but it is giving me error:我有一个简单的表单,我正在尝试提交,但它给了我错误:
TypeError: Cannot read property '0' of null.类型错误:无法读取 null 的属性“0”。
I removed almost everything but it is still giving me error.我删除了几乎所有内容,但它仍然给我错误。 I don't know why.我不知道为什么。
Here is the code.这是代码。
<body id="home" ng-app="formApp">
<div ng-controller="formRegister">
<form name="formprofile3" method="post" id="formprofile3" role="form" ng-submit="formprofile3()">
<!--<div class="form-group">
<input type="text" id="fname" name="fname" placeholder="Your Name" ng-model="formData.fname" title="Please Enter Your First Name" class="form-control input-sm" required="required">
</div>
<div class="form-group">
<input type="email" id="email" name="email" placeholder="Your Email" ng-model="formData.email" class="form-control input-sm" title="Please Enter Your Valid Email" required="required">
</div>
<div class="form-group">
<input type="text" name="subject" id="subject" placeholder="Subject" ng-model="formData.subject" title="Please enter Subject" class="form-control input-sm" required="required">
</div>
<div class="form-group">
<textarea name="message" id="message" placeholder="Message" title="Please enter your message/concern/query" ng-model="formData.msg" class="form-control input-sm" required="required"></textarea>
</div> -->
<div class="form-group">
<button type="submit" class="btn btn-home" name="btn-register" id="btn-register" required="required">Submit</button>
</div>
</form>
Angular Code.角码。
var formApp = angular.module('formApp', []);
formApp.controller('formRegister', function($scope,$http){
$scope.formData = {};
$scope.formprofile3 = function() {
alert('hi');
/* var allData={'formData': $scope.formData}
$http({
method : 'POST',
url : 'http://xyzcom/contact.php',
data : allData,
headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
})
.success(function(data) {
if (!data.success) {
$scope.message = data.errors.message;
}else{
$scope.message = '';
alert('Your message has been sent.');
location.href = './profile.html';
}
});*/
};
As you can see most of them are comment out but still I am getting error.正如您所看到的,它们中的大多数都被注释掉了,但我仍然遇到错误。
Here, form is considered as angular directive which instantiate FormController .在这里, form被认为是实例化FormController 的角度指令。 If the name
attribute of form
directive is specified, the form controller is published onto the current scope under this name.如果指定了form
指令的name
属性,则表单控制器将发布到此名称下的当前范围。 So when you're writing ng-submit function name same as form name attribute it try to (search and) execute in that newly created scope.因此,当您编写与表单名称属性相同的 ng-submit 函数名称时,它会尝试(搜索和)在新创建的范围内执行。 Name that function other than form name & issue will be solved.命名表单名称和问题以外的功能将被解决。
Example: Plunker (Change form name to same as ng-submit function to reproduce the issue).示例: Plunker (将表单名称更改为与 ng-submit 函数相同以重现该问题)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.