【发布时间】:2015-08-13 08:46:17
【问题描述】:
单击重置按钮时,我正在清除所有字段。但是,表单状态未设置为初始状态。有一些错误。如果它是 ng-valid 或 ng-invalid,我正在更改文本的颜色。一旦字段值被重置,如何将表单带入初始状态。
HTML 代码
<style>
input.ng-invalid {
border: 1px solid #FCD8C8;
}
input.ng-valid {
border: 1px solid #B3F7BC;
background-color: #E7F2E9;
}
</style>
<div ng-controller="DBController">
<div style="display:inline-block;vertical-align:top;aligh:right;float:right">
<button >Save</button>
<button ng-click="resetDashboard()">Reset </button>
</div>
<form name="DashboardForm" ng-model="DashboardForm" novalidate class="css-form">
<br>
<p>Title:
<input type="text" name="title" ng-model="dashboard.title" required ng-minlength="3" ng-maxlength="20">
<span style="color:red" ng-show="DashboardForm.title.$invalid">
<span ng-show="DashboardForm.title.$error.required">required.</span>
<span ng-show="DashboardForm.title.$error.minlength">Title is too short.</span>
<span ng-show="DashboardForm.title.$error.maxlength">Title is too long.</span>
</span>
<br>
<p>Description:
<input type="textarea" style="border-radius:3px;width:300px;height:30px" name="description" ng-model="dashboard.description" ng-minlength="3" ng-maxlength="1050" required>
<span style="color:red" ng-show="DashboardForm.description.$invalid">
<span ng-show="DashboardForm.description.$error.required">required.</span>
<span ng-show="DashboardForm.description.$error.minlength">Description is too short.</span>
<span ng-show="DashboardForm.description.$error.maxlength">Description is too long.</span>
</span>
</p>
</form>
</div>
JS 代码
;(function() {
"use strict";
angular.module('App')
.controller('DBController', ['$scope', '$log','$window',
function($scope, $log,window) {
$scope.title = null;
$scope.description = null;
var defaultdashboardForm = {
title : "",
description : ""
};
$scope.resetDashboard = function()
{
$scope.dashboard.title='';
$scope.dashboard.description='';
}
}]);
})();
【问题讨论】:
-
那是因为您使用了
ng-minlength,它不会在您的ng-model中增加价值。看看这个stackoverflow.com/a/28338146/2435473 会让您了解幕后发生的事情跨度> -
感谢 pankaj。现在我如何清除一个值但仍然有效。我设置为空。为什么在加载表单时没有发生这种情况。当时没有为文本框分配值。
-
你有没有仔细阅读过我的回答...只有你才会知道为什么会这样。
标签: angularjs