在 angularjs 中输入有效时添加 class
add class when input is valid in angularjs
我的 angularjs 表格有一个错误。
当来自 angularjs 表单的输入有效时,我想更改元素的 class,如果输入无效,则将其更改回来。
我得到了这个代码:
<li data-ng-class="{ success: userFormRegistration.email.$valid, danger: userFormRegistration.email.$invalid }" class="danger">valid email</li>
现在这是电子邮件的输入:
<input type="email" name="email" data-ng-model="userFormRegistration.email" class="form-control ng-pristine ng-invalid ng-invalid-required ng-invalid-email" placeholder="Email" required="">
一开始添加的 class 是 danger
但是当我输入一个有效的电子邮件时它删除了 class 但没有添加 success
class..
这里是 angularjs 代码:
// app.js
// create angular app
var FormApp = angular.module('FormApp', []); ;
// create angular controller
/*FormApp.controller('registrationFormController', function ($scope, $http) {*/
function registrationFormController($scope, $http) {
// create a blank object to hold our form information
// $scope will allow this to pass between controller and view
$scope.userFormRegistration = {};
// function to submit the form after all validation has occurred
$scope.submitForm = function (form) {
// check to make sure the form is completely valid
if (form.$valid) {
$scope.userFormRegistration = { actionname: "registration", email: $scope.userFormRegistration.email, password: $scope.userFormRegistration.password }
// start ajax call
$http({
method: 'POST',
url: 'Registration.php',
data: $.param($scope.userFormRegistration), // pass in data as strings
headers: { 'Content-Type': 'application/x-www-form-urlencoded'} // set the headers so angular passing info as form data (not request payload)
})
.success(function (data) {
console.log(data);
if (!data.success) {
// if not successful, bind errors to error variables
$scope.errorRegistrationEmail = data.errors.email2;
$scope.errorRegistrationPassword = data.errors.password;
$scope.errorRegistrationActionname = data.errors.actionname;
$scope.registrationMessage = data.message;
}
else {
// if successful, bind success message to message
$scope.message = data.message;
}
});
//end ajax call
}
else {
if (!$scope.userFormRegistration.email.$valid) {
var error = { message: "please enter valid email" };
$scope.errorRegistrationMessage = error.message;
}
else if (!$scope.userFormRegistration.password.$valid) {
var error = { message: "please enter valid password " };
$scope.errorRegistrationMessage = error.message;
}
}
};
};
请多指教
$valid 和 $invalid 属性来自表单而不是输入值。您需要一个带有名称的表单元素,然后在输入中也输入一个名称,然后使用 formName.inputName.$valid 或 $invalid.
我的 angularjs 表格有一个错误。 当来自 angularjs 表单的输入有效时,我想更改元素的 class,如果输入无效,则将其更改回来。
我得到了这个代码:
<li data-ng-class="{ success: userFormRegistration.email.$valid, danger: userFormRegistration.email.$invalid }" class="danger">valid email</li>
现在这是电子邮件的输入:
<input type="email" name="email" data-ng-model="userFormRegistration.email" class="form-control ng-pristine ng-invalid ng-invalid-required ng-invalid-email" placeholder="Email" required="">
一开始添加的 class 是 danger
但是当我输入一个有效的电子邮件时它删除了 class 但没有添加 success
class..
这里是 angularjs 代码:
// app.js
// create angular app
var FormApp = angular.module('FormApp', []); ;
// create angular controller
/*FormApp.controller('registrationFormController', function ($scope, $http) {*/
function registrationFormController($scope, $http) {
// create a blank object to hold our form information
// $scope will allow this to pass between controller and view
$scope.userFormRegistration = {};
// function to submit the form after all validation has occurred
$scope.submitForm = function (form) {
// check to make sure the form is completely valid
if (form.$valid) {
$scope.userFormRegistration = { actionname: "registration", email: $scope.userFormRegistration.email, password: $scope.userFormRegistration.password }
// start ajax call
$http({
method: 'POST',
url: 'Registration.php',
data: $.param($scope.userFormRegistration), // pass in data as strings
headers: { 'Content-Type': 'application/x-www-form-urlencoded'} // set the headers so angular passing info as form data (not request payload)
})
.success(function (data) {
console.log(data);
if (!data.success) {
// if not successful, bind errors to error variables
$scope.errorRegistrationEmail = data.errors.email2;
$scope.errorRegistrationPassword = data.errors.password;
$scope.errorRegistrationActionname = data.errors.actionname;
$scope.registrationMessage = data.message;
}
else {
// if successful, bind success message to message
$scope.message = data.message;
}
});
//end ajax call
}
else {
if (!$scope.userFormRegistration.email.$valid) {
var error = { message: "please enter valid email" };
$scope.errorRegistrationMessage = error.message;
}
else if (!$scope.userFormRegistration.password.$valid) {
var error = { message: "please enter valid password " };
$scope.errorRegistrationMessage = error.message;
}
}
};
};
请多指教
$valid 和 $invalid 属性来自表单而不是输入值。您需要一个带有名称的表单元素,然后在输入中也输入一个名称,然后使用 formName.inputName.$valid 或 $invalid.