How do I clear or reset data in a form in angularjs?

前端 未结 5 1304
误落风尘
误落风尘 2021-01-16 21:48

Been finding how to clear form data with the use of angular and $setPristine Function but still no results always gives me an error saying $setPristine is not a function. Ca

相关标签:
5条回答
  • 2021-01-16 22:05

    You can remove form field value by removing value of ng-model like

    Your code

    var CustDetails = {
                    cname: $scope.CusDetails.cname,
                    comname: $scope.CusDetails.comname,
                    tel: $scope.CusDetails.tel,
                    email: $scope.CusDetails.email
    
                }; 
    

    Replace with this

    $scope.CustDetails = {
                    cname: $scope.CusDetails.cname,
                    comname: $scope.CusDetails.comname,
                    tel: $scope.CusDetails.tel,
                    email: $scope.CusDetails.email
    
                }; 
    

    Your code

    customerForm.$setPristine(true);
    

    Replace with this

    $scope.CustDetails={};
    
    0 讨论(0)
  • 2021-01-16 22:06
    1. Use name attr instead of id. name="forName"
    2. The form reference inside Controller will be $scope.formName
    0 讨论(0)
  • 2021-01-16 22:06

    You can set the form with the code below:

    $scope.customerForm.$setPristine();
    $scope.customerForm.$setUntouched();
    $scope.CustDetails={};
    
    0 讨论(0)
  • 2021-01-16 22:17

    use:

    $scope.$destroy 
    

    as it removes all the children associated to the parent scope and will clear all the data associated with it.

    0 讨论(0)
  • 2021-01-16 22:26

    Use

    $scope.customerForm.$setPristine(true);
    

    Give the form a name like

    name= "CusDetails"
    

    Then It will solve the problem.

    0 讨论(0)
提交回复
热议问题