$ setPristine()不工作。 [英] $setPristine() not working.
问题描述
****注意我剪了我的一些其他功能,以使code可以通过更快的读取。我无法清除表单。当我点击的取消功能的按钮。我以为我可以设置默认的形式,但这并不有所作为。
<形式NAME =myForm会NOVALIDATE NG-提交=提交()>
<表类=mealCost> <! - 描述 - >
&所述; TR>
&所述; TD>基地豆粕价格:LT; / TD>
< TD><输入类型=数字NAME =价格NG-模式=mealPrice要求>< / TD>
< / TR>
<! - 服务员把在信息 - >
&所述; TR>
&所述; TD>税率:%LT; / TD>
< TD><输入类型=数字步=0.01NAME =税NG-模式=mealTax要求>< / TD> < / TR> &所述; TR>
&所述; TD>提示百分比:%LT; / TD>
< TD><输入类型=数字NAME =小费步=0.01NG模型=tipPercent要求>< / TD> < / TR> < /表> < p =类userResponse>
<输入类型=提交值=提交>
<! - <输入ID =取消类型=提交值=取消NG提交=取消(正本)> - >
<按钮NG点击=取消()>开始超过LT; /按钮>
&所述; / P>< /表及GT;
下面是我的javascript我想用命令按钮室内用NG-点击我的形式设置为$ setPristine。我虽然这设置默认的形式将有助于但没有发生在提交
VAR应用= angular.module('对myApp',[])。
控制器('costController',函数($范围){
// $ scope.ready = FALSE;
$ scope.mealPrice =;
$ scope.mealTax = 0.05;
$ scope.tipPercent = 0.05;
//可能可以做 VAR defaultForm = {
价钱: ,
税:,
小费:
}$ scope.cancel =功能(){
$ scope.myForm $ setPristine()。
$ scope.user = angular.copy(defaultForm);
的console.log('空');
}
我觉得你用错了。
$ setPristine:
这种方法可以称为以删除'NG-脏'类和表单设置为它的原始状态(NG-原始类)。这种方法也将传播到本表格所载的所有控制。
所以这唯一清楚的类,但不是$范围的变量。
你做重置$ scope.user变量,可以说:
添加用户。在HTML
每个模型前 NG-模式=user.tipPercent
NG-模式=user.mealTax
NG-模式=user.mealPrice
而在你的JS替换这样的:
// $ scope.ready = FALSE;
$ scope.mealPrice =;
$ scope.mealTax = 0.05;
$ scope.tipPercent = 0.05;
//可能可以做 VAR defaultForm = {
价钱: ,
税:,
小费:
}$ scope.cancel =功能(){
$ scope.myForm $ setPristine()。
$ scope.user = angular.copy(defaultForm);
的console.log('空');
}
这样:
VAR defaultForm = {
mealPrice:,
mealTax:0.05,
tipPercent:0.05
}$ scope.user = angular.copy(defaultForm);$ scope.cancel =功能(){
$ scope.myForm $ setPristine()。
$ scope.user = angular.copy(defaultForm);
的console.log('空');
}
**** Note I cut out some of my other functions so that the code could be read through faster. I cannot clear the form. when i click the button with the cancel function. I thought I could set a default form, but this doesn't make a difference.
<form name="myForm" novalidate ng-submit="submit()">
<table class="mealCost">
<!-- descriptions -->
<tr>
<td> Base Meal Price: </td>
<td><input type="number" name="price" ng-model="mealPrice" required></td>
</tr>
<!-- waiter puts in the info -->
<tr>
<td> Tax Rate: % </td>
<td><input type="number" step="0.01" name="tax" ng-model="mealTax" required></td>
</tr>
<tr>
<td> Tip Percentage: % </td>
<td><input type="number" name="tip" step="0.01" ng-model="tipPercent" required></td>
</tr>
</table>
<p class="userResponse">
<input type="submit" value="Submit">
<!-- <input id="cancel" type="submit" value="Cancel" ng-submit="cancel(original)"> -->
<button ng-click="cancel()">Start Over</button>
</p>
</form>
Here is my javascript I am trying to set my form to $setPristine using the button command wiht ng-click. I though that setting a default form would help but nothing happens on submit
var app = angular.module('myApp',[]).
controller('costController', function($scope) {
// $scope.ready= false;
$scope.mealPrice ="" ;
$scope.mealTax = 0.05;
$scope.tipPercent =0.05;
// possibly could do
var defaultForm={
price: "",
tax: "",
tip:""
}
$scope.cancel = function() {
$scope.myForm.$setPristine();
$scope.user = angular.copy(defaultForm);
console.log('empty');
}
I think you're using it wrong.
$setPristine:
"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."
So this only clear's classes but not the $scope variables. You do reset a $scope.user variable, could say:
Add 'user.' in front of every model in the Html
ng-model="user.tipPercent"
ng-model="user.mealTax"
ng-model="user.mealPrice"
And replace this in your JS:
// $scope.ready= false;
$scope.mealPrice ="" ;
$scope.mealTax = 0.05;
$scope.tipPercent =0.05;
// possibly could do
var defaultForm={
price: "",
tax: "",
tip:""
}
$scope.cancel = function() {
$scope.myForm.$setPristine();
$scope.user = angular.copy(defaultForm);
console.log('empty');
}
to this:
var defaultForm = {
mealPrice : "",
mealTax : 0.05,
tipPercent : 0.05
}
$scope.user = angular.copy(defaultForm);
$scope.cancel = function () {
$scope.myForm.$setPristine();
$scope.user = angular.copy(defaultForm);
console.log('empty');
}
这篇关于$ setPristine()不工作。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!