admin管理员组文章数量:1277887
I have taken a look at the following: Reset Form in AngularJS and Angular clear subform data and reset validation from these I am trying to create a dynamic form reset/clear function which would look like something like the following:
$scope.clearSection = function(formName){
$scope.formName.$setPristine();
$scope.formName.$setUntouched();
};
Is it possible to create a dynamic clear function like this in angular where the form name is dynamically passed to this function?
I have taken a look at the following: Reset Form in AngularJS and Angular clear subform data and reset validation from these I am trying to create a dynamic form reset/clear function which would look like something like the following:
$scope.clearSection = function(formName){
$scope.formName.$setPristine();
$scope.formName.$setUntouched();
};
Is it possible to create a dynamic clear function like this in angular where the form name is dynamically passed to this function?
Share Improve this question edited May 23, 2017 at 12:13 CommunityBot 11 silver badge asked Nov 12, 2015 at 8:13 DeanMWakeDeanMWake 9233 gold badges19 silver badges38 bronze badges3 Answers
Reset to default 6Example plunkr: http://plnkr.co/edit/lnGym0vKsANL6oks30RG
$scope.resetForm = function(formName) {
var form = $scope[formName];
form.$setUntouched();
form.$setPristine();
}
Note: You still need to reset the input values!
According to the docs:
https://docs.angularjs/api/ng/type/form.FormController
$setPristine(); Sets the form to its pristine state.
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.
Setting a form back to a pristine state is often useful when we want to 'reuse' a form after saving or resetting it.
$setUntouched(); Sets the form to its untouched state.
This method can be called to remove the 'ng-touched' class and set the form controls to their untouched state (ng-untouched class).
Setting a form controls back to their untouched state is often useful when setting the form back to its pristine state.
$setPristine and $setUntouched only change the classes of the form controls, not the values. That means you still need to reset the values.
I don't see why not.
$scope.clearSection = function(formName){
var frm = $scope[formName];
frm.$setPristine();
frm.$setUntouched();
};
Well yes.
In JavaScript you can access variables of an object using the []
notation. Take the following example
var str = 'hello';
var obj = { hello: 'a' };
console.info(obj[str]); // outputs 'a'
so if formName is a string, you can simply get the property of the scope using
var form = $scope[formName]
本文标签: javascriptDynamically reset ngform in AngularStack Overflow
版权声明:本文标题:javascript - Dynamically reset ng-form in Angular - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741238477a2363495.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论