问题描述
我正在尝试制作一个包含基本输入(如联系电话,姓名和电子邮件地址)的表单。该想法是当用户单击刷新按钮时刷新“返回新表单”。下面是我的要求
最初在第一次执行代码时..它加载页面并从范围中获取默认值。单击刷新按钮后,表单应重新显示。但是,表单无法从中加载默认值最初执行时的范围。
下面发布的是我的代码
<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>
1楼
在这行上:
var stu= $scope.refresh();
您正在通过使用括号调用$ scope.refresh,从而清除了表单。
它应该是:
var stu= $scope.refresh;
虽然我不确定您在使用stu。
2楼
经过一番适当的代码检查,我能够提出解决方案。
$scope.refresh=function(){
$scope.name='';
$scope.contact='';
$scope.emailid='';
var stu= $scope.refresh();
return stu;
}
});
好吧,下面的早期部分曾经在括号之外
var stu= $scope.refresh();
return stu;
现在,我将其保留在括号内