如果您输入框中只有空内容,那么我一定是个错误。当你点击我的按钮时必须先告诉它。
当特定错误发生时,我应该像boostrap一样添加错误类。我该怎么做?
var app = angular.module('UserValue', [])
app.controller('UserFirstValue', function ($scope) {
var mail = $scope.emai;
var password = $scope.password;
var firstname = $scope.Firstname;
var lastname = $scope.Lastname;
var age = $scope.age;
$scope.CheckUserValue = function () {
if (mail == null && password == null && firstname == null && lastname == null && age == null) {
//show error her
return "Remember to throw content in!"
}
return null;
}
});
create.cshtml:
<div class="col-lg-6" ng-app="UserValue" ng-controller="UserFirstValue">
//Show error her
<div class="OutError" ng-show="CheckUserValue()" style="margin-bottom:10px;">
<button type="submit" class="btn btn-success" ng-click="CheckUserValue()" id="clickUserCreate"><i class="fa fa-plus"></i></button>
</div>
</div>
错误是: 它没有显示字幕向前的方法。
答案 0 :(得分:0)
好的 - 所以你在这里遇到了多个问题......我只想在这里留下一个JS垃圾箱供你查看,它应该完全解决你之前尝试设置验证的问题。
https://jsbin.com/nanizafizi/1/edit?html,js,console,output
HTML:
<div class="col-lg-6" ng-app="UserValue" ng-controller="UserFirstValue" ng-hide="hideForm">
{{errorMessage}}
<form ng-submit="submitForm()" role="form">
<div class="form-group">
<label>First Name:</label>
<input type="text" ng-model="user.first" class="form-control">
</div>
<div class="form-group">
<label>Last Name:</label>
<input type="text" ng-model="user.last" class="form-control">
</div>
<div class="form-group">
<label>Email address:</label>
<input type="email" ng-model="user.email" class="form-control">
</div>
<div class="form-group">
<label>Password:</label>
<input type="password" ng-model="user.password" class="form-control">
</div>
<div class="form-group">
<label>Age:</label>
<input type="number" class="form-control" ng-model="user.age">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
JS:
var app = angular.module('UserValue', [])
app.controller('UserFirstValue', function ($scope) {
$scope.errorMessage = "";
$scope.hideForm = false;
$scope.submitForm = function() {
if ($scope.user === undefined ||
$scope.user.first === undefined ||
$scope.user.last === undefined ||
$scope.user.email === undefined ||
$scope.user.password === undefined ||
$scope.user.age === undefined) {
$scope.errorMessage = "Oops it looks like you missed a field!";
}
else {
$scope.user = {};
$scope.hideForm = true;
}
}
});