我喜欢HTML:
<select id="example">
<option value="1">Short</option>
<option value="2">Oh dear, this option has really long text :(</option>
我想将以下jquery添加到角度控制器中,我该怎么做?
var maxLength = 15;
$('#example > option').text(function(i, text) {
if (text.length > maxLength) {
return text.substr(0, maxLength) + '...';
}
});
答案 0 :(得分:1)
你真的不应该这样做,这违反了AngularJS的工作方式,并且会让任何其他看你代码的AngularJS开发人员感到沮丧。
相反,更改为将选项列表声明为控制器中作用域的属性,并修改其中的文本以将其截断为所需的长度。
e.g。
标记:
<select id="example" ng-options="option.value as option.text for option in options">
控制器代码:
var maxLength = 15;
$scope.options = ['Short', 'Oh dear, this option has really long text :('].map(function (text, i) {
return {
value: i + 1,
text: text.length <= maxLength ? text : text.substring(0, maxLength) + '...'
};
});