AngularJS - expandable textarea

Boris

Using an Angular directive, I'm trying to create a vertically expandable textarea that has the same number of rows as the number of '\n' characters in the text.

(Note, I'm aware that this won't work for wrapping lines - I'll deal with this separately).

app.directive('textExpand', function() {
  return {
    restrict: 'A',
    require: 'ngModel',
    link: function(scope, elem, attrs, ngModel) {

      RecalculateLines();

      function RecalculateLines() {
        scope.textEntry = "line1\nline2\n\nline3";
        angular.element(elem).attr("rows", (scope.textEntry).split("\n").length);
      }

      elem.on('keydown', function(event) {

        RecalculateLines();

      });
    }
  };
});

The idea is that I have a function to constantly recount the number of line breaks and dynamically update the number of rows using angular's jqLite to set the attribute. I call that function once in the beginning (to display any pre-existing values) and then once on every keystroke.

See plunk here.

(scope.textEntry).split("\n").length gets correctly counted when loading the hardcoded value and updated when typing into the box. However, setting it using .attr() only seems to work on the initial call and not on keystrokes.

What's weirder is that .attr() seems to work fine if passing it another number. See commented out code in the plunk where the box expands and shrinks fine based on a counter every keystroke.

What am I missing?

shaunhusain

You were resetting the scope variable in the string that was checking it, moving this out of the function and adding ng-trim="false" fixes the problem.

http://plnkr.co/edit/W0g1PupXYTUgiNLK1D8M?p=preview

  function RecalculateLines() {
    angular.element(elem).attr("rows", (scope.textEntry).split(/\r?\n/g).length);
    // angular.element(elem).attr("rows", rowCount);
  }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Angularjs expandable table row

From Dev

AngularJS : Expandable recursive tree table

From Dev

create a dynamic expandable table by using angularjs and jquery

From Dev

Highlight currently selected node in expandable tree angularJS

From Dev

How to make <textarea>s un-expandable using HTML?

From Dev

Tinymce textarea not working with AngularJS

From Dev

Getting value of TextArea angularjs

From Dev

angularjs : @mention in textarea

From Dev

AngularJS Render HTML in textarea

From Dev

Tinymce textarea not working with AngularJS

From Dev

disable a textarea with angularjs

From Dev

Limiting words in textarea with AngularJS

From Dev

Set limit to Textarea in AngularJs

From Dev

Angularjs with ng-repeat and ng-show index(expandable list)

From Dev

Update textarea rows via Angularjs

From Dev

Get Textarea Cursor Position in AngularJs

From Dev

Angularjs: return remaining characters in textarea

From Dev

Angularjs textarea not processing line breaks

From Dev

Angularjs: return remaining characters in textarea

From Dev

readonly textarea to fit the content with angularjs

From Dev

AngularJS textarea newline on enter key

From Dev

Get value of TextArea in AngularJS service

From Dev

Default textarea value in angularjs when textarea is bound to model

From Dev

TinyMCE <textarea> two way bound with AngularJS

From Dev

AngularJS Validation - ng-minlength on textarea

From Dev

Binding value into TinyMCE <textarea> using AngularJS model

From Dev

Angularjs directive to Insert text at textarea caret

From Dev

AngularJS with Breeze - Highlight textarea when required

From Dev

AngularJS: Writing to and Reading from textarea with multilines

Related Related

HotTag

Archive