javascript - $watch element.scrollHeight not triggered -
i have div
inside td
tag (which inside tr
tag). div
has custom directive needs $watch
scrollheight
of element:
<tr ng-show="showrow"> <td> <div my-custom-directive>my long text</div> </td> </tr>
and directive:
appdrct.directive('mycustomdirective', [function() { return { restrict : 'a', compile : function(elem, attr, linker) { return function(scope, element, attributes) { scope.$watch(element[0].scrollheight, function(){ console.log('triggered'); }) } } } }])
the showrow
variable changes through ng-click
.
the problem $watch
never triggered (except first time) though scrollheight
changes due ng-show
(scrollheight
equal 0 when ng-show
equal false
).
i first thought because had use $scope.$apply
don't know put it...
you cannot watch direct object reference (which you're trying do). either watch property of current scope (by providing name of property string), or watch function, returns new value.
change watch to:
scope.$watch(function(){ return element[0].scrollheight }, function(){ console.log('triggered'); });
on note, commenters point out, should reference directive using dashes, rather camel-case, in dom.
Comments
Post a Comment