简体   繁体   English

单击angular js中的刷新按钮时无法清除详细信息?

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

i'm trying to make a form which takes the basic inputs like contact number,name and email address.The idea is to get refreshed "Return a fresh form" when the user clicks on the refresh button.Below mentioned are my requirements 我正在尝试制作一个包含基本输入(如联系电话,姓名和电子邮件地址)的表单。该想法是当用户单击刷新按钮时刷新“返回新表单”。下面是我的要求

Initally when the code is executed for the first time ..it the page loads taking the default values from the scope.As soon as i click the refresh button the form should turn fresh.However,the form is unable to load the default values from the scope when it is executed intially. 最初在第一次执行代码时..它加载页面并从范围中获取默认值。单击刷新按钮后,表单应重新显示。但是,表单无法从中加载默认值最初执行时的范围。

Below posted is my code 下面发布的是我的代码

 <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>

On this line: 在这行上:

var stu= $scope.refresh();

You are calling $scope.refresh by using the parentheses, thus clearing out your form. 您正在通过使用括号调用$ scope.refresh,从而清除了表单。

It should be: 它应该是:

var stu= $scope.refresh;

Though I'm not sure what you're using stu for. 虽然我不确定您在使用stu。

After a bit of proper code inspection,I am able to come up with the solution. 经过一番适当的代码检查,我能够提出解决方案。

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

Well,Earlier below part used to be outside of parenthesis 好吧,下面的早期部分曾经在括号之外

var stu= $scope.refresh();
return stu; 

Now,I have kept it inside the paranthesis 现在,我将其保留在括号内

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM