Javascript 仅在单击“角度”菜单上的“保存”按钮后验证输入字段
我想验证我的输入必填字段,并仅在单击表单中的保存按钮(提交)后显示错误消息。 在angularjs中,最好的方法是什么 谢谢,请检查这个 您可以在该链接中找到已解释的示例Javascript 仅在单击“角度”菜单上的“保存”按钮后验证输入字段,javascript,html,angularjs,validation,Javascript,Html,Angularjs,Validation,我想验证我的输入必填字段,并仅在单击表单中的保存按钮(提交)后显示错误消息。 在angularjs中,最好的方法是什么 谢谢,请检查这个 您可以在该链接中找到已解释的示例 module = angular.module('app', []); module.directive('showErrors', function() { return { restrict: 'A', require: '^form', link: function (sco
module = angular.module('app', []);
module.directive('showErrors', function() {
return {
restrict: 'A',
require: '^form',
link: function (scope, el, attrs, formCtrl) {
// find the text box element, which has the 'name' attribute
var inputEl = el[0].querySelector("[name]");
// convert the native text box element to an angular element
var inputNgEl = angular.element(inputEl);
// get the name on the text box
var inputName = inputNgEl.attr('name');
// only apply the has-error class after the user leaves the text box
inputNgEl.bind('blur', function() {
el.toggleClass('has-error', formCtrl[inputName].$invalid);
})
}
}
});
module.controller('NewUserController', function($scope) {
$scope.save = function() {
if ($scope.userForm.$valid) {
alert('User saved');
$scope.reset();
} else {
alert("There are invalid fields");
}
};
$scope.reset = function() {
$scope.user = { name: '', email: '' };
}
});
您尝试过的内容\请参见以下内容: