我有一个表单,我需要在$ pristine字段上显示错误。如果我只专注于那个领域而没有输入任何东西而集中注意力。 但是从开始就不应该看到错误。
这是我的fiddle。
这是我的代码......
<div ng-app>
<form action="" name="myForm">
<div>
<input type="text" ng-model="name" name="n">
<span ng-show="myForm.n.$pristine">please fill something</span>
</div>
<div>
<input type="text" ng-model="email">
<span ng-show="">please fill something</span>
<span ng-show="">invalid password</span>
</div>
<div>
<input type="text" ng-model="cemail">
<span ng-show="">please fill something</span>
<span ng-show="">password dosent match</span>
</div>
<div>
<input type="submit">
</div>
</form>
</div>
答案 0 :(得分:1)
<强>被修改强>
对不起,但我的网络连接速度很慢,我无法编辑 jsfiddle ,但是我从你的小提琴代码中为你剪了一个。请参阅下面的代码。
请参阅以下编辑的代码。需要考虑的一些方面:
<div ng-app>
我将其替换为<div ng-app="app">
function TodoCtrl($scope) {}
。我用语法angular.module('app', []).controller('TodoCtrl', TodoCtrl);
function TodoCtrl($scope) {
$scope.visible = [];
$scope.show = function(form, input) {
if (form && input) {
if (form[input].$pristine && !form[input].$modelValue) {
$scope.visible[form[input].$name] = true;
}
}
}
}
angular.module('app', []).controller('TodoCtrl', TodoCtrl);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
<div ng-app="app" ng-controller="TodoCtrl">
<form action="" name="myForm">
<div>
<input type="text" ng-model="name" name="n" ng-blur="show(myForm, 'n')">
<span ng-show="visible['n']">please fill something</span>
</div>
<div>
<input type="text" ng-model="email" name="myEmail" ng-blur="show(myForm, 'myEmail')">
<span ng-show="visible['myEmail']">please fill something</span>
<span ng-show="">invalid password</span>
</div>
<div>
<input type="text" ng-model="cemail" name="cemail" ng-blur="show(myForm, 'cemail')">
<span ng-show="visible['cemail']">please fill something</span>
<span ng-show="">password dosent match</span>
</div>
<div>
<input type="submit">
</div>
</form>
</div>
&#13;
答案 1 :(得分:0)
使用可以在输入文本框中使用required
,如下所示
<div>
<input type="text" ng-model="name" name="n" required>
<span ng-show="myForm.n.$pristine || myForm.n.$error.required">please fill something</span>
</div>
希望这有助于!!