0

I have a directive that I am currently making that is an input field of type text. Now I would like this field's width to grow dynamically if the text gets too big for the input field. Below is my directive:

.directive('dynamicInput', function () {
    return {
        restrict: 'E',
        template: '<input type="text" style="display: inherit;" class="form-control" required />',
        require: 'ngModel',
        link: function (scope, element, attrs, ngModel) {


            console.log('ATTRS: ', attrs);
            console.log('ELEMENT: ', element);

            if(attrs.width){
                console.log('WiDTH: ', attrs);

            }

        }
    }
});

Here is the plunker:

Change Width Dynamically.

I know you can change the CSS class youre using on your element object, however, I dont just want to change the class I want to basically dynamically change the width as the text increases inside the box. So the question is: How can I change the CSS on every fire of the 'onchange' event to the length of the text being entered? Also, I would like to keep this contained within a directive, so that I am not relying on anything within the parent scope in which its declared in.

2
  • 1
    use ng-change and put change handler in directive link function where you also have access to the element Commented Oct 14, 2014 at 13:04
  • @charliefl the ng-change would definitely do the change event, however, it wouldnt help with updating the style on the input tag. Commented Oct 14, 2014 at 13:52

1 Answer 1

1

You can get your input element, and then do whatever you want with it using vanilla javascript or angular object element.

Like this (link function):

var inputEl = angular.element(element.children()[0]);

inputEl.on('keydown', function() {
   inputEl.attr('size', inputEl.val().length); 
});

This does not do exactly what you want I think, but you get the idea. You have access to the element inside the directive, so this kind of logic is very easy to implement without depending on anything else but itself.

Modified plunker.

Sign up to request clarification or add additional context in comments.

4 Comments

is angular.element(element.children()[0]); the best way to grab the attribute for the input element. I cant just use the attrs in the link function?
Is there anyway to get the style for the input, so I can change the width? Rather then change the size attribute?
I was thinking and this does satisfy the dynamic growth of text input, however, my question was about changing css dynamically as I need to change the width and color of the input.
You are using your directive as an element, so your attrs refers to your directive element instead of the input (not sure I'm clear). Do a console.log(element) to see for yourself. As for accessing the css, you can by using inputEl.css(name, value), you can refer to the angular.element documentation to see all the things you can do. And if it's not enough, you can get the "regular" dom element with angular.element[0] and use regular javascript, like domElement.style.css, and so on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.