Comments (8)
HI CodingAspect!
I just had a problem with initial resize (textarea value is initialized from ng-model).
When the code in link function executes, the $element[0].value is not set yet.
Here is my fix:
//if($element[0].value != ''){
//$scope.autogrowFn();
//}
$timeout(function(){
$scope.autogrowFn();
});
And you could add:
$window.addEventListener('resize', adjustHeight, false);
to handle browser window resize event.
I needed as simple as possible directive to do the job, but I couldn't find anything sensible...
And your code / idea is great, so I borrowed it... :-]
Thanks!
Here is my directive - I think there is no need to put everything on the $scope.
I am not sure, if addEventListener code is called only once.
(function(){
'use strict';
angular.module('angular-adjustheight', [])
.directive('adjustheight', ['$window', '$timeout', function($window, $timeout){
var getElementOffset = function(element) {
var style = $window.getComputedStyle(element, null),
props = ['paddingTop', 'paddingBottom'],
offset = 0;
for(var i=0; i<props.length; i++){
offset += parseInt(style[props[i]]);
}
return offset;
};
var adjustElementHeight = function(element, offset) {
var newHeight = 0;
element.style.overflowY = 'hidden';
element.style.height = 'auto';
newHeight = element.scrollHeight - offset;
element.style.height = newHeight + 'px';
};
return {
link: function($scope, $element, $attrs) {
var element = $element[0];
var offset = getElementOffset(element);
var setHeight = function() {
adjustElementHeight(element, offset);
}
element.addEventListener('input', setHeight, false);
$window.addEventListener('resize', setHeight, false);
$timeout(function(){
setHeight();
});
}
}
}]);
})();
https://plnkr.co/edit/r43DDnw68ZB9qX0O18Tm?p=preview
greetings
Mike
from angularjs-autogrow.
Yes, it should.
Can you provide more details (angular version for example)?
Thanks.
from angularjs-autogrow.
I just tried it and works. Did you tried in a simple textarea? I did and works but I tried in textareas generated dynamically in a ng-repeat and doesn't work.
https://dl2.pushbulletusercontent.com/InnTDKJMTpahq7J5Fw93W3Wgc3PCmlPc/Screenshot_2016-06-21-15-56-54.png (the red one is the normal textarea)
from angularjs-autogrow.
hmm...interesting, probably the render should be push with $timeout.
I'll try to reproduce this bug and come back with a fix.
from angularjs-autogrow.
Besides the content comes from a model. The block of that example is this one:
<li class="step" ng-repeat="step in photo.receipeStepsTemp">
<div><span class="step-number abs">{{$index+1}}</span></div>
<textarea id="instructions" placeholder="Write instructions..." ng-model="step.text" autogrow></textarea>
<div id="add-photo">
Add photo
</div>
</li>
from angularjs-autogrow.
Thanks @Mike120Git, works perfectly :)
The only thing I see is not ok is when you have to deal with textareas with
box-sizing: border box
and width: 100%
Edit: I jus fixed it with this:
element.style.height = newHeight + 8 + 'px';
The only problem is the initial size, it's too big
from angularjs-autogrow.
@codingAspect Did you ever come up with a fix for textareas dynamically generated in an ng-repeat? Been using your directive and it works great outside of that issue!
from angularjs-autogrow.
@Mike120Git - I'm not sure why my comment isn't here, but this issue has been fixed right after you posted it.
@justinabbott I tried to reproduce the issue on Angular v1.4 and it works perfectly with ng-repeat.
Please review my Plunker and see what you do differently.
from angularjs-autogrow.
Related Issues (9)
- Auto-resize on load? HOT 2
- text jump issue in IE 11
- Missing License info HOT 1
- Very slow when typing and holding specific key
- contenteditable div with autogrow does not work in IE 11 HOT 2
- Grow Up? (Catchy Title :-) HOT 2
- Height should be updated if the font-family changes HOT 1
- Doesn't update on directive load with pre-existing text HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from angularjs-autogrow.