Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$watch for element width inside directive

I'm trying to $watch my element's width inside a directive:

appDrct.directive('setWidth', function($interval) {
    return {
        require: '?ngModel',
        link: function($scope, $elm, attr, ngModel) {

            $scope.$watch($elm.parent().width(), function() {
                console.log('Width changed');
            });
        }
     }
})

But it doesn't work...I know for a fact that my width changes (I tried with $interval to display the width and it changes)

like image 306
ncohen Avatar asked May 08 '15 17:05

ncohen


1 Answers

Here's how you do it, you need to $watch the function expression:

$scope.$watch(function () {
        return $element[0].style.width;
       }, function(newVal, oldVal) {
        console.log('Width changed');
});

Fiddle

like image 99
Omri Aharon Avatar answered Oct 06 '22 07:10

Omri Aharon