Javascript 使用Minng Length验证时角度特性定义不足
在我的角度控制器中,我尝试验证用户是否插入了5个字符,如果少于5个字符,则应显示警告。我的控制器:Javascript 使用Minng Length验证时角度特性定义不足,javascript,angularjs,angular1.6,Javascript,Angularjs,Angular1.6,在我的角度控制器中,我尝试验证用户是否插入了5个字符,如果少于5个字符,则应显示警告。我的控制器: var app = angular.module('myapp', ['ui.bootstrap']); app.controller('FeedController', function ($scope, $http) { $scope.customPostText = ""; $scope.sendMessage = function() { con
var app = angular.module('myapp', ['ui.bootstrap']);
app.controller('FeedController', function ($scope, $http) {
$scope.customPostText = "";
$scope.sendMessage = function()
{
console.log("text ");
console.log($scope.customPostText);
var length = $scope.customPostText.length
//could us:
//if ($scope.postcommentForm.customPostText.$valid) instead to check if valid
//but I want to see the length.
if(length >4 && length < 255)
{
alert("not undefined");
}
else
{
alert("message needs 5 characters you have."+length);
}
}
});
如何使用ng minlength而不存在值未定义的问题?最小长度验证可防止在内容无效时发送内容。 因此,您只能得到未定义的
var app = angular.module('myapp', ['ui.bootstrap']);
app.controller('FeedController', function ($scope, $http) {
$scope.customPostText = "";
$scope.sendMessage = function()
{
console.log("text ");
console.log($scope.postcommentForm.customPostText);
var length = $scope.postcommentForm.customPostText.length
if(length >4 && length < 255)
{
alert("not undefined");
}
else
{
alert("Bericht moet minimaal 5 karakters bevatten.");
}
}
处理这一问题的一种方法是:
var length=angular.isDefined($scope.customPostText)$scope.customPostText.length:0
这样,只要在输入字段中没有写入有效的输入,您就会得到0。是的,问题是因为您使用的ng minlength,只有当它超过min length时,才会将值分配给ng模型。我认为你无法改变这种行为 这并不理想,但临时解决方案是删除最小长度,您可以做的是在控制器中创建一个类似于触发器的变量
if(length >4 && length < 255)
{
$scope.length_valid = true;
alert("not undefined");
}
else
{
$scope.length_valid = false;
alert("message needs 5 characters.");
}
在js中为变量指定如下表单名称:$scope.postcommentForm.customPostText 它永远不会给你未定义的
var app = angular.module('myapp', ['ui.bootstrap']);
app.controller('FeedController', function ($scope, $http) {
$scope.customPostText = "";
$scope.sendMessage = function()
{
console.log("text ");
console.log($scope.postcommentForm.customPostText);
var length = $scope.postcommentForm.customPostText.length
if(length >4 && length < 255)
{
alert("not undefined");
}
else
{
alert("Bericht moet minimaal 5 karakters bevatten.");
}
}
var-app=angular.module('myapp',['ui.bootstrap']);
app.controller('FeedController',函数($scope,$http){
$scope.customPostText=“”;
$scope.sendMessage=函数()
{
控制台日志(“文本”);
log($scope.postcommentForm.customPostText);
变量长度=$scope.postcommentForm.customPostText.length
如果(长度>4&&长度<255)
{
警报(“未定义”);
}
其他的
{
警报(“贝里赫特·莫特最低5卡拉克特·贝瓦滕”);
}
}
}))
ng-class="{ 'success-border': postcommentForm.customPostText.$valid && length_valid ,'error-border': !postcommentForm.customPostText.$valid || !length_valid }"
var app = angular.module('myapp', ['ui.bootstrap']);
app.controller('FeedController', function ($scope, $http) {
$scope.customPostText = "";
$scope.sendMessage = function()
{
console.log("text ");
console.log($scope.postcommentForm.customPostText);
var length = $scope.postcommentForm.customPostText.length
if(length >4 && length < 255)
{
alert("not undefined");
}
else
{
alert("Bericht moet minimaal 5 karakters bevatten.");
}
}