Skip to content

Commit

Permalink
Add maxlength attribute which will be applied to the input field
Browse files Browse the repository at this point in the history
  • Loading branch information
Chuck Haines committed Mar 17, 2015
1 parent 2f53f08 commit 786e862
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ var app = angular.module('app', ["angucomplete-alt"]);
| :------------- |:-------------| :-----:| :-----|
| id | A unique ID for the field. [example](http://ghiden.github.io/angucomplete-alt/#example1) | Yes | members |
| placeholder | Placeholder text for the search field. [example](http://ghiden.github.io/angucomplete-alt/#example1) | No | Search members |
| maxlength | Maxlength attribute for the search field. [example](http://ghiden.github.io/angucomplete-alt/#example1) | No | 25 |
| pause | The time to wait (in milliseconds) before searching when the user enters new characters. [example](http://ghiden.github.io/angucomplete-alt/#example1) | No | 400 |
| selected-object | Either an object in your scope or callback function. If you set an object, it will be two-way-bound data as usual. If you set a callback, it gets called when selection is made. [example](http://ghiden.github.io/angucomplete-alt/#example1) | Yes | selectedObject or objectSelectedCallback |
| remote-url | The remote URL to hit to query for results in JSON. angucomplete will automatically append the search string on the end of this, so it must be a GET request. [example](http://ghiden.github.io/angucomplete-alt/#example5) | No | http://myserver.com/api/users/find?searchstr= |
Expand Down
6 changes: 5 additions & 1 deletion angucomplete-alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
var KEY_TAB = 9;

var MIN_LENGTH = 3;
var MAX_LENGTH = 524288; // the default max length per the html maxlength attribute
var PAUSE = 500;
var BLUR_TIMEOUT = 200;

Expand All @@ -48,7 +49,7 @@
// Set the default template for this directive
$templateCache.put(TEMPLATE_URL,
'<div class="angucomplete-holder" ng-class="{\'angucomplete-dropdown-visible\': showDropdown}">' +
' <input id="{{id}}_value" ng-model="searchStr" ng-disabled="disableInput" type="{{type}}" placeholder="{{placeholder}}" ng-focus="onFocusHandler()" class="{{inputClass}}" ng-focus="resetHideResults()" ng-blur="hideResults($event)" autocapitalize="off" autocorrect="off" autocomplete="off" ng-change="inputChangeHandler(searchStr)"/>' +
' <input id="{{id}}_value" ng-model="searchStr" ng-disabled="disableInput" type="{{type}}" placeholder="{{placeholder}}" maxlength="{{maxlength}}" ng-focus="onFocusHandler()" class="{{inputClass}}" ng-focus="resetHideResults()" ng-blur="hideResults($event)" autocapitalize="off" autocorrect="off" autocomplete="off" ng-change="inputChangeHandler(searchStr)"/>' +
' <div id="{{id}}_dropdown" class="angucomplete-dropdown" ng-show="showDropdown">' +
' <div class="angucomplete-searching" ng-show="searching" ng-bind="textSearching"></div>' +
' <div class="angucomplete-searching" ng-show="!searching && (!results || results.length == 0)" ng-bind="textNoResults"></div>' +
Expand Down Expand Up @@ -654,6 +655,9 @@
// set strings for "Searching..." and "No results"
scope.textSearching = attrs.textSearching ? attrs.textSearching : TEXT_SEARCHING;
scope.textNoResults = attrs.textNoResults ? attrs.textNoResults : TEXT_NORESULTS;

// set max length (default to maxlength deault from html
scope.maxlength = attrs.maxlength ? attrs.maxlength : MAX_LENGTH;

// register events
inputField.on('keydown', keydownHandler);
Expand Down
2 changes: 1 addition & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ <h2>Awesome Autocompleteness for AngularJS</h2>

<h3><a name="example1" class="page-anchor">Example 1 - Countries of the World</a></h3>
<div class="padded-row">
<div angucomplete-alt id="ex1" placeholder="Search countries" pause="100" selected-object="selectedCountry" local-data="countries" search-fields="name" title-field="name" minlength="1" input-class="form-control form-control-small" match-class="highlight">
<div angucomplete-alt id="ex1" placeholder="Search countries" maxlength="50" pause="100" selected-object="selectedCountry" local-data="countries" search-fields="name" title-field="name" minlength="1" input-class="form-control form-control-small" match-class="highlight">
</div>
</div>
<div class="result">
Expand Down
8 changes: 8 additions & 0 deletions test/angucomplete-alt.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ describe('angucomplete-alt', function() {
$scope.$digest();
expect(element.find('#ex1_value').attr('placeholder')).toEqual('Search countries');
});

it('should render maxlength string', function() {
var element = angular.element('<div angucomplete-alt id="ex1" placeholder="Search countries" selected-object="selectedCountry" local-data="countries" search-fields="name" title-field="name" maxlength="25" />');
$scope.selectedCountry = null;
$compile(element)($scope);
$scope.$digest();
expect(element.find('#ex1_value').attr('maxlength')).toEqual('25');
});

});

Expand Down

0 comments on commit 786e862

Please sign in to comment.