我试图禁用复选框旁边的按钮,但前提是复选框的基础模型与原始值不同。Pristine 本身是行不通的,因为从技术上讲,形式已经改变。我的解决方案是获取原始值并根据当前值是否等于该值来切换表单的原始值。
我假设有一个我不知道的更好的方法。
<div ng-controller="MyCtrl">
<div ng-form="form">
<div class="checkbox">
<label>
<input type="checkbox" ng-click="toggle(form)" ng-model="foo.bar">{{ foo.bar }}
</label>
</div>
<button class="btn btn-primary" ng-disabled="form.$pristine" ng-click="save(form)">Button</button>
</div>
</div>
angular.module("app", [])
.controller("MyCtrl", function($scope, $timeout){
//catch when scope is first set
$scope.$watch("foo", function(curr, prev){
if(curr && !prev)
$scope.original = curr.bar;
});
//assume this is being set by some other scope
$timeout(function(){
$scope.foo = {
bar: true
};
}, 500);
$scope.toggle = function(form){
if($scope.original == $scope.foo.bar)
form.$setPristine();
console.log(form.$pristine);
};
$scope.save = function(form){
$scope.original = $scope.foo.bar;
form.$setPristine();
};
});