Javascript 角度$setPristine不工作
Html: 我使用控制器作为语法Javascript 角度$setPristine不工作,javascript,angularjs,Javascript,Angularjs,Html: 我使用控制器作为语法 <form name="occupantDetailForm" role="form" novalidate class="form-validation"> <div class="form-group form-md-line-input form-md-floating-label no-hint"> <input class="form-control" type="text" n
<form name="occupantDetailForm" role="form" novalidate class="form-validation">
<div class="form-group form-md-line-input form-md-floating-label no-hint">
<input class="form-control" type="text" name="LastName" ng-model="vm.occupantDetail.lastName" ng-class="{'edited':vm.occupantDetail.lastName}" maxlength="@OccupantDetail.MaxLength" required>
<label>@L("LastName")</label>
</div>
<button type="submit" class="btn btn-primary blue" ng-click="vm.saveOccupantDetail(occupantDetailForm)" ng-disabled="occupantDetailForm.$invalid"><i class="fa fa-save"></i> <span>@L("Save")</span></button>
</form>
Q:我尝试了很多方法,但都不管用?当我使用
vm.occountdetailform.$setUntouched()时代码>那么它工作正常。但是问题是保存
按钮没有被禁用。你能告诉我为什么吗?当我使用vm.occountdetailform.$setPristine()时代码>只有这样它才不会工作。为什么?谢谢 $setPristine
仅将您的表单标记为$pristine
,若要实际重置所需的表单,请将模型设置为新对象
链接中给出了更好的解释:
下面是一些可能对您有所帮助的代码:
<div ng-app="myapp">
<div ng-controller="UserCtrl">
<form name="user_form" novalidate>
<input name="name" ng-model="user.name" placeholder="Name" required/>
<button class="button" ng-click="reset()">Reset</button>
</form>
<p>
Pristine: {{user_form.$pristine}}
</p>
</div>
</div>
小提琴:
更新:,能否尝试将$submitted
设置为false
$scope.occupantDetailForm.$setPristine();
$scope.occupantDetailForm.$setUntouched();
$scope.occupantDetailForm.$submitted = false;
您是否尝试过occupentDetailForm.$invalid | | occupentDetailForm.$dirty?是否尝试重置表单?@HristoGeorgiev-Nope,我为什么要使用它?如果您查看上面的代码片段,我已经这样做了。此处vm.occupentDetail={}代码>我需要在没有任何验证错误等情况下将表单设置为初始状态。您将表单设置为初始状态的方式是我认为导致您出现问题的原因。。。vm.occountdetail={};vm.occountdetailform.$setPristine();我正在使用控制器作为语法。
。不像您上面提到的那样。谢谢。这很好,sampath,vm.yourobj={}在$setPristine()之后就可以了。
var app = angular.module('myapp', []);
function UserCtrl($scope) {
$scope.reset = function() {
$scope.user = {};
$scope.user.name = "";
$scope.user_form.$setPristine();
$scope.user = {};
}
}
$scope.occupantDetailForm.$setPristine();
$scope.occupantDetailForm.$setUntouched();
$scope.occupantDetailForm.$submitted = false;