gpt4 book ai didi

javascript - http回调后Angularjs $setPristine

转载 作者:太空宇宙 更新时间:2023-11-04 16:08:59 26 4
gpt4 key购买 nike

我发现下面的代码很奇怪。 dataSvc.saves 回调之后,vm.bookDetails = {};和 vm.bookForm.$setPristine 的用途?

angular.module('controllers',[])
.controller('SecondController', function(dataSvc){
var vm=this;

vm.saveData = function () {
dataSvc.save(vm.bookDetails).then(function(result) {
vm.bookDetails = {};
vm.bookForm.$setPristine();
});
};

vm.numberPattern = /^\d*$/;
});

代码来自here

最佳答案

ng-dirty 类告诉您表单已被用户修改,而 ng-pristine 类告诉您表单尚未被用户修改。所以 ng-dirty 和 ng-pristine 是同一个故事的两个方面。

这些类可以在任何字段上设置,而表单有两个属性:$dirty 和 $pristine。

来自Angular.org

$setPristine 表示

Sets the form to its pristine state.

This method sets the form's $pristine state to true, the $dirty state to false, removes the ng-dirty class and adds the ng-pristine class. Additionally, it sets the $submitted state to false.

您可以使用 $scope.form.$setPristine() 函数将表单重置为原始状态。 $setPristine() 是在 angularjs 的 1.1.x 分支中引入的

代码说明

angular.module('controllers',[])
.controller('SecondController', function(dataSvc){
var vm=this;

vm.saveData = function () {
dataSvc.save(vm.bookDetails).then(function(result) {
// This code will reset the form/clear value of all ngmodel
vm.bookDetails = {};
// This code will set the validity of the form to invalid state, submitted cannot be made if pristine is set.
vm.bookForm.$setPristine();
});
};

vm.numberPattern = /^\d*$/;
});

关于javascript - http回调后Angularjs $setPristine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41660788/

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