Created
October 10, 2013 18:47
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<span> | |
<input type="text" ng-model="value"> | |
<span style="visibility:hidden; position:absolute; left:-1000; top:-1000;">{{value}}</span> | |
</span> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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'); | |
} | |
}); | |
} | |
}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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