I think you're using it wrong.
$setPristine:
"This method can be called to remove the 'ng-dirty' class and set the form to its pristine state (ng-pristine class). This method will also propagate to all the controls contained in this form."
So this only clears classes but not the $scope variables.
You do reset a $scope.user variable, could say:
Add 'user.' in front of every model in the Html
ng-model="user.tipPercent"
ng-model="user.mealTax"
ng-model="user.mealPrice"
And replace this in your JS:
// $scope.ready= false;
$scope.mealPrice ="" ;
$scope.mealTax = 0.05;
$scope.tipPercent =0.05;
// possibly could do
var defaultForm={
price: "",
tax: "",
tip:""
}
$scope.cancel = function() {
$scope.myForm.$setPristine();
$scope.user = angular.copy(defaultForm);
console.log('empty');
}
to this:
var defaultForm = {
mealPrice : "",
mealTax : 0.05,
tipPercent : 0.05
}
$scope.user = angular.copy(defaultForm);
$scope.cancel = function () {
$scope.myForm.$setPristine();
$scope.user = angular.copy(defaultForm);
console.log('empty');
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…