解决方法
基于@ notme的答案,我为自己的自动调整大小输入角度指令版本创建了以下要点:
https://gist.github.com/Zmaster/6923413
这是代码:
模板:
<span> <input type="text" ng-model="value"> <span style="visibility:hidden; position:absolute; left:-1000; top:-1000;">{{value}}</span> </span>
指示:
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'); } }); } }; });