Jquery 表单thorughs需要字段错误,因此我在AngularJS中按入列表后清除模型
我的表单中有两个必填字段。当我单击“添加”按钮时,模型值将推送到一个列表,并使用ng repeat显示。在那之后,它通过required字段错误,因此我将两个模型都清除为空 请检查我的样本代码Jquery 表单thorughs需要字段错误,因此我在AngularJS中按入列表后清除模型,jquery,angularjs,Jquery,Angularjs,我的表单中有两个必填字段。当我单击“添加”按钮时,模型值将推送到一个列表,并使用ng repeat显示。在那之后,它通过required字段错误,因此我将两个模型都清除为空 请检查我的样本代码 <body ng-controller="testcontrollerApp"> <div ng-app="testcontroller"> <form name="TestForm" id="TestForm"> <div> <
<body ng-controller="testcontrollerApp">
<div ng-app="testcontroller">
<form name="TestForm" id="TestForm">
<div>
<ul class="remove-style">
<li ng-repeat="test in tests">
<span>Name : {{test.name}}</span>
<span>ID : {{test.id}}</span>
</li>
</ul>
<div class="inputs"><span>Name</span><span><input type="text" ng-model="item.txtname" required/></span></div>
<div class="inputs"><span>Id</span><span><input type="text" ng-model="item.txtid" required/></span></div>
<input type="submit" ng-click="btnclick()" value="Add" />
</div>
<script type="text/javascript">
debugger;
var testcontrollerApp = angular.module('testcontrollerApp', []);
testcontrollerApp.controller("testcontroller", function ($scope) {
$scope.tests = [
{ 'name': 'Test1', 'id': 'id1' }
];
$scope.btnclick = function () {
debugger;
if ($scope.TestForm.$valid !== false) {
$scope.tests.push({ 'name': $scope.item.txtname, 'id': $scope.item.txtid });
$scope.item.txtname = '';
$scope.item.txtid = '';
$scope.TestForm.$setPristine();
}
}
});
</script>
</form></div></body>
-
名称:{{test.Name}
ID:{{test.ID}
名称
身份证件
调试器;
var testcontrollerApp=angular.module('testcontrollerApp',[]);
testcontrollerApp.controller(“testcontroller”,函数($scope){
$scope.tests=[
{'name':'Test1','id':'id1'}
];
$scope.btnclick=函数(){
调试器;
if($scope.TestForm.$valid!==false){
$scope.tests.push({'name':$scope.item.txtname,'id':$scope.item.txtid});
$scope.item.txtname='';
$scope.item.txtid='';
$scope.TestForm.$setPristine();
}
}
});
Moveng单击
从输入到ng提交
,格式如下:
ng-submit="btnclick()"
使用
input type="reset" value="Add"
谢谢你的回答,它工作得很好,我是这个AngularJS的初学者。:)欢迎来到堆栈溢出!虽然此代码可能有助于解决问题,但它没有解释为什么和/或如何回答问题。提供这种额外的环境将大大提高其长期价值。请在回答中添加解释,包括适用的限制和假设。