gpt4 book ai didi

javascript - Angular ngOptions 切片或椭圆

转载 作者:行者123 更新时间:2023-11-28 19:09:12 24 4
gpt4 key购买 nike

我在 ngOptions 中显示了一些非常长的句子,我从 api 填充这些句子。我怎样才能将它们切片,这样我最终会得到类似(红色...或蓝色...)的东西,上面有点词的结尾...,嗯,我可能应该尝试创建一个过滤器,在其中我应该获取 str 的长度,然后根据我想要的字符数对其进行切片...任何帮助都值得赞赏。

$scope.colors = [
{name:'blacksomerandomwords', shade:'dark'},
{name:'whitesomerandomwords', shade:'light', notAnOption: true},
{name:'redsomerandomwords', shade:'dark'},
{name:'bluesomerandomwords', shade:'dark', notAnOption: true},
{name:'yellowsomerandomwords', shade:'light', notAnOption: false}
];
<select ng-model="myColor" ng-options="color.name for color in colors"></select>

最佳答案

yourApp.filter('short', function() { 
// pass in collection, property to shorten and max length
return function(values, property, length) {
angular.forEach(values, function(value) {
value[property] = value[property].length <= length ?
value[property] :
value[property].substr(0, length) + '...';
});
return values;
};
});

<select ng-model="myColor"
ng-options="color.name for color in colors | short:'name':6">
</select>

相关的plunker在这里http://plnkr.co/edit/Mvky0H

关于javascript - Angular ngOptions 切片或椭圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31045152/

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