AngularJS directive input width resize by keyup

前端 未结 5 795
余生分开走
余生分开走 2021-01-06 06:36

I created a directive in order to make a input that has width automatically resized when keyup (like Google Contacts). However it seems not to be ok, because the width of ea

5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-06 07:03

    Based on @notme's answer I created the following gist for my own version of an auto-resizing input angular directive:

    https://gist.github.com/Zmaster/6923413

    Here is the code:

    Template:

    
      
      {{value}}
    
    

    Directive:

    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');
              }   
            }); 
          }   
        };  
      });
    

提交回复
热议问题