gpt4 book ai didi

angularjs - Angular 1.2 : Is it possible to exclude an input on form dirty checking?

转载 作者:行者123 更新时间:2023-12-04 00:03:57 27 4
gpt4 key购买 nike

在下面的示例中,是否可以忽略下拉列表的脏状态?现在,如果用户更改所选人员,就会变得很脏。但我不在乎这个字段在我的表单验证中是否脏。

function TestingCtrl($scope) {
$scope.company = '';
$scope.persons = [{
name: 'Alice'
}, {
name: 'Bob'
}];


$scope.selectedPerson = $scope.persons[0];

$scope.checkForm = function() {
if ($scope.personForm.$dirty) {
alert('Form is dirty');
} else {
alert('Form is clean');
}
}

}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js"></script>


<div ng-app>

<div ng-controller="TestingCtrl">

<form name="personForm" novalidate>
Company:
<input type="text" ng-model="company" required>
<br>Persons:
<select ng-options="p.name for p in persons" ng-model="selectedPerson"></select>

</form>

<br>
<button ng-click="checkForm()">Check if dirty</button>

</div>

</div>

最佳答案

boindiil 的基于指令的解决方案有效但有一个缺陷:如果表单的 $setPritine 会停止工作是手动执行的。这可以通过添加一个额外的行来解决输入的方法行为:

angular.module('myApp', []).directive('ignoreDirty', [function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, elm, attrs, ctrl) {
ctrl.$setPristine = function() {};
ctrl.$pristine = false;
}
}
}]);

关于angularjs - Angular 1.2 : Is it possible to exclude an input on form dirty checking?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28721959/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com