本文介绍了动态ng-最小长度/ng-最大长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ng-minlength&&ng-maxlength.

I'm trying to add dynamic min/maxlength attributes using ng-minlength && ng-maxlength.

例如,

<input type="text" ng-minlength="myMin" ng-maxlength="myMax">

在我的控制器中...

And in my controller...

$scope.myMin = 1;
$scope.myMax = 2;

但是,这不起作用.最终,我希望能够更新这些属性并使输入反映它们的值.这有可能吗?

However, this is not working. Ultimately, I want to be able to update these attributes and have the input reflect their values. Is this even possible?

随便的例子:

http://plnkr.co/edit/KkUvFjSZkvFwqZVZAIiS?p=info

推荐答案

您需要添加ng模型.请记住,这不会限制输入.仅当最大和最小值有效/无效时才给您提供类.

You need to add an ng model. Keep in mind that this will not limit the input. Only give you classes when the max and min values are valid/invalid.

<input type="text" ng-model="number" ng-minlength="myMin" ng-maxlength="myMax">

app.controller('MainCtrl', function($scope) {
  $scope.myMin = 1;
  $scope.myMax = 5;
  $scope.number = 1;
});

如果您只想限制输入,则可以使用常规html:

If you just want to limit the input you can use regular html:

<input type="text" ng-model="number" maxlength="{{myMax}}" >

我用两个示例创建了一个叉子.

I created a fork with both examples.

这篇关于动态ng-最小长度/ng-最大长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 18:30