A
A
Artem Shchurin2015-09-04 12:26:35
JavaScript
Artem Shchurin, 2015-09-04 12:26:35

Why doesn't the $watch in the directive watch the $scope variables?

Hello!

<div ng-controller="ParametersCtrl as ctrl">
    <div class="modal-resize" resize-parameters ng-click="ctrl.resize()">Раскрыть</div>
    <!--Разметка-->
</div>

Piece from the controller
this.resize = function() {
      $scope.fullScreen = !$scope.fullScreen;
    };

Directive
define(['app'], function(app) {
  app.directive('resizeParameters', function() {
    return {
      restrict: 'A',
      link: function($scope, element, attrs) {
        $scope.$watch($scope.fullScreen, function(fullScreen) {
console.log('resize');
        })
      }
    }
  })
});

$scope is not isolated, ctrl.resize() works, console.log('resize') is called only at initialization

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vsvladimir, 2015-09-04
@schurin

It looks like you need to change it like this:

$scope.$watch($scope.fullScreen, function(fullScreen)

$scope.$watch("fullScreen", function(fullScreen)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question