在angular js中,点击刷新按钮时无法清除细节

Unable to clear the details when clicking the refresh button in angular js?

本文关键字:按钮 细节 清除 刷新 js angular      更新时间:2023-09-26

我试图使一个形式,需要基本的输入,如联系电话,姓名和电子邮件地址。这样做的目的是为了恢复精神当用户单击刷新按钮时,"返回一个新的表单"。以下是我的要求

当代码第一次执行时。如果页面加载时使用范围中的默认值。只要我点击刷新按钮,表单就会刷新。但是,表单在初始执行时无法从作用域加载默认值。

下面是我的代码

 <div ng-app="ang2" ng-controller="form_controller">
 <form name="registration" novalidate>
 Name:<input type = "text" ng-model="name" name="name" required>
 Contact:<input type = "text" ng-model="contact" name="contact"required>
 Email-address: <input type = "text" ng-model="emailid"   
 name="emailid"required>
 <button ng-click="refresh()">Refresh</button>
 </form>
 </div>
 <script>
  var ang2=angular.module("ang2",[]);
   ang2.controller('form_controller',function($scope){
   $scope.name='rishi';
   $scope.contact='4437577391';
   $scope.emailid='rishanthkanakadri@gmail.com';
   $scope.refresh=function(){
   $scope.name='';
   $scope.contact='';
   $scope.emailid='';
   }
  var stu= $scope.refresh();
  return stu;
   });
 </script>

这一行:

var stu= $scope.refresh();

你正在调用$scope。使用括号刷新,从而清除表单。

应该是:

var stu= $scope.refresh;

虽然我不确定你用stu做什么

经过一些适当的代码检查后,我能够提出解决方案。

$scope.refresh=function(){
$scope.name='';
$scope.contact='';
$scope.emailid='';
var stu= $scope.refresh();
return stu; 
}
});

前面的部分是在括号

之外的
var stu= $scope.refresh();
return stu; 

现在,我把它放在括号