I realize that there are SO posts out there asking similar or even the same thing, but before you mash the duplicate button, be aware I have tried the solution from every single one, and none of them have worked in my target AngularJS version (1.4.8). With that in mind:
HTML:
<div ng-app="app" ng-controller="app">
<textarea size-watch>{{ height }}</textarea>
</div>
JavaScript:
angular.module("app", [])
.controller("app", function() {
})
.directive("sizeWatch", function() {
return {
restrict: "A",
link: function(scope, elem) {
scope.$watch(function() { return elem[0].offsetHeight; }, function(newHeight, oldHeight) {
scope.height = newHeight;
}, true);
}
}
});
Here's what I'm trying to do: I want to create a directive that watches the height of an element and updates a property on the scope whenever it changes. I can get the value to initially display, so I know the link between the directive and the controller/scope is working. However, I can't get the value to update when the textarea is resized (by dragging the bottom right corner). I put a call to debugger;
in the second watch
function, and verified that it's only getting called once.
Feel free to update the linked fiddle when you find a working solution.