简体   繁体   English

ng-repeat过滤器,条件角为js

[英]ng-repeat filter with condition angular js

This is my code : 这是我的代码:

 <option ng-repeat="name in names" value="{{names.name}">
    {{names.name | limitTo:10 }}
 </option>

How can I add a condition: 我该如何添加条件:

if( names.name.length > 10) { 
     //add ... point after name displayed 
}

to get result <option value="Alexander Bradley">Alexander ... </option> 获得结果<option value="Alexander Bradley">Alexander ... </option>

Any ideas? 有任何想法吗?

You can create your own filter that does this and include the length parameter if you like. 您可以创建自己的过滤器来执行此操作,并根据需要包含length参数。

.filter("ellipses", function () {
    return function (item) {
        if (item.length > 10) {
            return item.substring(0, 10) + " ..."
        }
        return item;
    };
});

<option ng-repeat="name in names" value="{{names.name}">
    {{names.name | ellipses }}
</option>

You can include the length argument as well and even use limitTo itself: 您也可以包含length参数,甚至可以使用limitTo本身:

.filter("ellipses", ["$filter", function ($filter) {
    return function  (item, limit) {
        if (item.length > limit) {
            return $filter("limitTo")(item, limit) + " ...";
        }
        return item;
    };
}]);

I think this is what you asked for.. Limiting the name to 10 chars and then adding a "..." to the end of it if it exceeds 10 characters. 我认为这就是你要求的..如果超过10个字符,将名称限制为10个字符,然后在其末尾添加“...”。 This is how I would do it. 我就是这样做的。 (Also fixed some of your syntax errors). (还修复了一些语法错误)。

<option ng-repeat="name in names" value="{{name}}">
    {{name.length <= 10 ? name : name.substring(0, 10) + "..."}}
</option>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM