我不知道如何在 Angular 稳定版 1.0.8 中做到这一点。问题出在带有验证的表单中,我在其中输入了一个值并按下重置按钮我有不必要的消息“无效价格”(我知道该表单是 $dirty,所以我应该设置 $pristne)。在角度 1.1.1 中,我可以调用:
$scope.advertForm.$setPristne();
但是我如何在 angular 1.0.8 中替换它。
我的表格:
<form ng-controller="AddAdvertController" name="advertForm" novalidate>
<label>Price ($)</label>
<input ng-model="newAdvert.price" type="number" name="price" min="100" required>
<span ng-show="advertForm.price.$dirty && advertForm.price.$invalid">Invalid price</span>
<br/>
<button ng-click="reset()" ng-disabled="isUnchanged()">Reset</button>
<button ng-click="add()" ng-disabled="advertForm.$invalid || isUnchanged()">Save</button>
</form>
我的控制器:
motoAdsApp.controller('AddAdvertController', ['$scope',
function($scope) {
$scope.emptyAdvert = {
price: null
};
$scope.add = function() {
alert('User added!');
$scope.reset();
};
$scope.reset = function() {
$scope.newAdvert = angular.copy($scope.emptyAdvert);
if ($scope.advertForm) {
// In angular 1.1.1 or higher
//$scope.advertForm.$setPristne();
// How to do in angular 1.0.8?
}
};
$scope.isUnchanged = function() {
return angular.equals($scope.newAdvert, $scope.emptyAdvert);
};
$scope.reset();
}
]);