Skip to content

Instantly share code, notes, and snippets.

@Zmaster
Created October 10, 2013 18:47
Show Gist options
  • Save Zmaster/6923413 to your computer and use it in GitHub Desktop.
Save Zmaster/6923413 to your computer and use it in GitHub Desktop.
This is an AngularJS directive for having an automatically resizing text input that has two way data binding. Most of the credit goes to the plunkers in this questions (http://stackoverflow.com/questions/17410896/angularjs-directive-input-width-resize-by-keyup) for coming up with the starting point for the directive.
<span>
<input type="text" ng-model="value">
<span style="visibility:hidden; position:absolute; left:-1000; top:-1000;">{{value}}</span>
</span>
'use strict';
angular.module('autoSizeInput', [])
.directive('autoSizeInput', function() {
return {
replace: true,
scope: {
value: '=inputValue'
},
templateUrl: 'templates/directives/autoSizeInput.html',
link: function(scope, element, attrs) {
var elInput = element.find('input');
var elSpan = element.find('span');
elSpan.html(elInput.val());
scope.$watch('value', function(value) {
if(value) {
elSpan.html(elInput.val());
elInput.css('width', (elSpan[0].offsetWidth + 10) + 'px');
}
});
}
};
});
<div input-value="parameter.value" auto-size-input></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment