如何显示文本长度,即使它超过ng最大长度
How to show text length even if it exceed ng-maxlength?
我看不到ng-maxlength
达到的字符数。
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="plunker" ng-controller="MainCtrl">
<form name="form" novalidate>
<input type="text" ng-model="myChar" ng-maxlength="5">
<br>
<h1>Length: {{myChar.length}}</h1>
</form>
</div>
这是因为超过maxlength
之后,myChar
的$modelValue
将未定义。您仍然可以使用$viewValue
属性,如下例所示。
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.test = function() {
console.log($scope.form.myChar.$viewValue.length);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<div ng-app="plunker" ng-controller="MainCtrl">
<form name="form" novalidate>
<input type="text" name="myChar" ng-model="myChar" ng-maxlength="5" ng-keyup="test()">
<br>
<h1>Length: {{myChar.length}}</h1>
</form>
</div>
只是改进了@John Smith的答案:
你可以把他的验证直接放在你的html表达式上。然后,您可以使用一些CSS对其进行样式设置,以向用户显示长度无效:
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<style>
.red {
color: red;
}
</style>
<div ng-app="plunker" ng-controller="MainCtrl">
<form name="form" novalidate>
<input type="text" name="myChar" ng-model="myChar" ng-maxlength="5">
<br>
<h1 ng-class="{'red': form.myChar.$viewValue.length > 5}">Length: {{form.myChar.$viewValue.length}}</h1>
</form>
</div>
相关文章:
- ng隐藏和ng显示无法正常工作
- 根据特定条件使用ng显示/ng隐藏来显示/隐藏元素
- ng显示与变量的比较
- ng显示“;调用方法“;不起作用
- 角度方向ng显示不工作
- 有棱角的ng显示与动画.css
- ng显示ng隐藏在ng中,重复如何为每个包裹的块单击/触发
- AngularJS自定义指令ng显示/ng隐藏
- 模型更新时触发ng显示
- ng显示基于其他一些html元素的模型值
- AngularJS ng显示表达式在加载时不起作用
- 如何在使用$http缓存时重新触发ng-if/ng显示动画
- Angular 1.2.x ng隐藏和ng显示css转换
- Safari中Angular ng显示的按钮闪烁
- 我想使用ng重复数组值设置ng显示
- AngularJS-ng显示范围变量不起作用
- 为什么这个简单的ng绑定和ng显示不起作用
- 延迟后触发ng显示
- Ionic:ng显示和页面转换
- 角度ng显示不会在示波器更新时更新