如果用户在提交后从文本框中删除了文本,则仅在提交后应用角度验证,而不显示验证。 我的要求是技术上的, - >如果用户输入文本,则不应显示任何验证 - >仅在用户点击提交后才显示验证 &安培; - >提交后,如果用户触摸该文本框(或)从文本框中删除文本,则验证消息不应显示
你能告诉我解决方案, 快速的回复表示赞赏,如果你给我提供小提琴,那么我会非常感激。提前致谢
答案 0 :(得分:0)
我不确切知道你在寻找什么,但我想这段代码可能很有用。
它只是一个带有控制器和一些经过验证的字段的基本形式。 在此示例中,每个字段都是必需的,但您可以看到没有向用户提供验证反馈。
仅当表单有效并且随后使用标志(true / false)注册提交时,验证才会通过。 如果该标志为true且用户触摸其中一个字段,则所有字段再次为空。
控制器
(function(){
var Controller = function($scope){
// Parameters
var isFormSubmitted = false;
// Methods
// Shared Methods
$scope.checkFocus = function(){
if(!isFormSubmitted){
return;
}
// Reset all fields
$scope.fields = null;
// Do something
}
$scope.validate = function(){
isFormSubmitted = true;
// Do some validation
};
};
Controller.$inject = [
'$scope'
];
app.controller('MainCtrl', Controller);
})();
视图
<div ng-controller="MainCtrl">
<form name="form" ng-submit="form.$valid && validate()" novalidate>
<input ng-focus="checkFocus()" ng-model="fields.username" type="text" required placeholder="Username">
<input ng-focus="checkFocus()" ng-model="fields.password" type="password" required placeholder="Password">
<input type="submit" value="Submit">
</form>
</div>
答案 1 :(得分:0)
您可以使用标志来解决问题。假设您有一个包含输入的表单。单击提交按钮时,必须将标志isSubmitted
设置为true。并在输入更改时将其设为false。然后根据该标志显示验证消息:
这是一个假设的控制器:
function appCtrl($scope) {
$scope.isSubmitted = false;
$scope.submit = function() {
$scope.isSubmitted = true;
//...
}
$scope.inputChanged = function() {
$scope.isSubmitted = false;
}
}
这可能是你观点的一部分:
<input name="inputName" type="text" ng-model="myModel" ng-change="inputChanged()" required="" />
<span ng-show="form.$error.required && form.$dirty && isSubmitted">name is required</span>