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" 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>
JS:
vm.saveOccupantDetail = function (form) {
vm.occupantDetailForm = form;
createOrEditOccupantDetail();//create or edit
vm.occupantDetail = {};
vm.occupantDetailForm.$setPristine();
}
问: 我尝试了很多方法,但它不起作用?当我使用vm.occupantDetailForm.$setUntouched();
时,它工作正常。但问题是Save
按钮没有被禁用。你能告诉我为什么吗?当我使用vm.occupantDetailForm.$setPristine();
时,它根本不起作用。为什么?谢谢。
答案 0 :(得分:0)
$setPristine
仅将您的表单标记为$pristine
,并实际reset
表示您需要的表单,将model
设置为new object
。
链接中给出了更好的解释: $setPristine not working
以下是一些可能对您有用的代码:
<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>
控制器代码:
var app = angular.module('myapp', []);
function UserCtrl($scope) {
$scope.reset = function() {
$scope.user = {};
$scope.user.name = "";
$scope.user_form.$setPristine();
$scope.user = {};
}
}
小提琴: http://jsfiddle.net/p7e1nway/1/
更新:,您可以尝试将$submitted
设置为false
$scope.occupantDetailForm.$setPristine();
$scope.occupantDetailForm.$setUntouched();
$scope.occupantDetailForm.$submitted = false;