我有以下几点:
<div>{{modal.title}}</div>
有没有一种方法可以限制字符串的长度,比如20个字符?
一个更好的问题是,有没有一种方法可以将字符串更改为截断并显示。如果超过20个字符?
我有以下几点:
<div>{{modal.title}}</div>
有没有一种方法可以限制字符串的长度,比如20个字符?
一个更好的问题是,有没有一种方法可以将字符串更改为截断并显示。如果超过20个字符?
当前回答
我也遇到过类似的问题,我是这样做的:
{{ longString | limitTo: 20 }} {{longString.length < 20 ? '' : '...'}}
其他回答
如果你有两个绑定{{item.name}}和{{item.directory}}。
并且希望将数据显示为一个后跟名称的目录,假设目录为“/root”,名称为“Machine”(/root- Machine)。
{{[item.directory]+[isLast ? '': '/'] + [ item.name] | limitTo:5}}
这可能不是来自脚本末尾,但你可以使用下面的css并将这个类添加到div中。这将截断文本,并在鼠标悬停时显示全文。你可以添加更多的文本,并添加一个角点击手柄来改变cli上的div类
.ellipseContent {
overflow: hidden;
white-space: nowrap;
-ms-text-overflow: ellipsis;
text-overflow: ellipsis;
}
.ellipseContent:hover {
overflow: visible;
white-space: normal;
}
更优雅的解决方案:
HTML:
<html ng-app="phoneCat">
<body>
{{ "AngularJS string limit example" | strLimit: 20 }}
</body>
</html>
角代码:
var phoneCat = angular.module('phoneCat', []);
phoneCat.filter('strLimit', ['$filter', function($filter) {
return function(input, limit) {
if (! input) return;
if (input.length <= limit) {
return input;
}
return $filter('limitTo')(input, limit) + '...';
};
}]);
演示:
http://code-chunk.com/chunks/547bfb3f15aa1/str-limit-implementation-for-angularjs
ng-keypress = " filterValue(事件)”ng-model = "客户。CUSTOMER_PHONE”
美元的范围。filterValue = function($event){
if(isNaN(String.fromCharCode($event.keyCode)) ){
$event.preventDefault();
}
if($scope.customer.CUSTOMER_PHONE.length <= 11)
{
$scope.customer.CUSTOMER_PHONE = $scope.customer.CUSTOMER_PHONE;
}
else
{
$event.preventDefault();
}
};
我使用了一套非常有用的滤镜库“Angular-filter”,其中一个叫做“truncate”的也很有用。
https://github.com/a8m/angular-filter#truncate
用法是:
text | truncate: [length]: [suffix]: [preserve-boolean]