达到输入maxLength后,Ng样式更改边框颜色

时间:2017-11-21 09:23:58

标签: angularjs

我正在尝试在输入达到限制时更改边框颜色。我用ng-style和条件表达式做这个。在我的代码中,当它被认为是黑色时,它给了我一个红色边框。当达到它的极限时它会变红。

最佳解决方案是什么?



 <textarea rows="4" cols="40" ng-model="vm.formData.text" ng-style="{'border-color' : vm.valueLength > 950 ? 'red':'black'}" maxlength="{{vm.valueLength}}" class="form-control" id="text" placeholder="Bericht..."></textarea>
&#13;
select
sum(case when condition then A_amount end) as S_A_amount,
sum(case when condition then B_amount end) as S_B_amount
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

条件vm.valueLength > 950将始终为真,因为您将vm.valueLength的值设置为1000.所以您需要做的是检查输入长度是否大于950.您可以执行此操作这是使用ng-modelvm.formData.text.length > 950

&#13;
&#13;
var app=angular.module('plunker', []);
app.controller('ListCtrl',ListCtrl);
function ListCtrl($scope) {
  
var vm = this;
vm.valueLength = 1000;
vm.formData ={};
  
  
}
&#13;
<!doctype html>
<html ng-app="plunker">

<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.js"></script>
</head>

<body ng-controller="ListCtrl as vm">
    <div>
        <textarea rows="4" cols="40" ng-model="vm.formData.text" ng-style="{'border-color' : vm.formData.text.length > 950 ? 'red':'black'}" maxlength="{{vm.valueLength}}" class="form-control" id="text" placeholder="Bericht..."></textarea>
    </div>
</body>

</html>
&#13;
&#13;
&#13;