gpt4 book ai didi

angularjs - 如何以 Angular 更改最大长度值

转载 作者:行者123 更新时间:2023-12-01 08:54:16 25 4
gpt4 key购买 nike

我对 Angular 很陌生。我试图在点击时将 maxlength 的值从 300 更改为 140。这些按钮是使用 ng-repeat 加载的,第一个是唯一应该将值更改为 140 的按钮,其余的应该回到 300。

这是我的 Controller 中的内容:

//character counter
$scope.counter = function() {
var myEl = angular.element(document.querySelector('.form-control'));
myEl.attr('maxlength', '150');
};

我的 html 是这样的:

 <textarea data-ng-model="view.post.content" ng-trim="false" maxlength="340" class="form-control" style="height: 100px;"></textarea>

最佳答案

只需使用 ng-maxlength 并将其绑定(bind)到范围上的属性,这将提供验证安全性。

例子:-

 <textarea data-ng-model="view.post.content" ng-trim="false" 
ng-maxlength="maxValue" class="form-control"
style="height: 100px;"></textarea>

如果你想真正限制,那么只需使用插值maxlength={{maxValue}}。即

 <textarea data-ng-model="view.post.content" ng-trim="false" 
maxlength="{{maxValue}}" class="form-control"
style="height: 100px;"></textarea>

所以最初 $scope.maxValue = 340 然后在计数器内部将属性的值设置为 150。

Doc

ngMaxlength (optional) number : Sets maxlength validation error key if the value is longer than maxlength. Setting the attribute to a negative or non-numeric value, allows view values of any length.

Example

关于angularjs - 如何以 Angular 更改最大长度值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31467863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com