【发布时间】:2017-01-23 13:34:21
【问题描述】:
我正在使用“ui-select-macth”来添加多个值。有什么办法可以设置每次输入的文本字段的最大长度,如果可以,你能帮帮我吗?
【问题讨论】:
标签: angularjs
我正在使用“ui-select-macth”来添加多个值。有什么办法可以设置每次输入的文本字段的最大长度,如果可以,你能帮帮我吗?
【问题讨论】:
标签: angularjs
好的,有你的问题。
以下是 HTML 代码:
<div ng-app="ngrepeatSelect" ng-controller="ExampleController">
<form name="myForm">
<label for="repeatSelect"> Repeat select: </label>
<select name="repeatSelect" id="repeatSelect" ng-model="data.repeatSelect">
<option ng-repeat="option in data.availableOptions" value="{{option.id}}">{{option.name | truncate:3 }}</option>
</select>
</form>
<hr>
<tt>repeatSelect = {{data.repeatSelect}}</tt><br/>
</div>
以下是 Angular js 代码。我使用了 ui-select 匹配和选项以及 ng-repeat 以及过滤器。
angular.module('ngrepeatSelect', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.data = {
repeatSelect: null,
availableOptions: [
{id: '1', name: 'Option A'},
{id: '2', name: 'Option B'},
{id: '3', name: 'Option C'}
],
};
}])
.filter('truncate', function () {
return function (text, length, end) {
if (isNaN(length))
length = 10;
if (end === undefined)
end = "";
if (text.length <= length || text.length - end.length <= length) {
return text;
}
else {
return String(text).substring(0, length-end.length) + end;
}
};
});
Click here 去上面的代码演示。
【讨论】: