3

how do i watch a variable, that is bind to a parent controller?

function config($stateProvider) {
    $stateProvider
        .state('home', {
            url: '/',
            templateUrl: 'home.html',
            controller: 'HomeController',
            controllerAs: 'vm'
        })

        .state('home.child', {
            url: '/child',
            templateUrl: 'child.html',
            controller: 'ChildController',
            controllerAs: 'vm'
        });
}

function HomeController($scope) {
    this.title = 'Some Title';
}

function ChildController($scope) {
    var vm = this;
    $scope.$watch('vm.title', function(current, original) {
        $log.info('title was %s', original);
        $log.info('title is now %s', current);
    });

}

The watch-Function doesn't recognize changes on the parent title..

thanks! :)

2 Answers 2

6

Just access the $parent scope when referencing objects belonging to the parent. Also get used to a cleaner and more standardized way of structuring your controller JS.

HTML

<html ng-app="myApp">
  <body>
    <div ng-controller="HomeController">
      <input type="text" ng-model="title">
      <div ng-controller="ChildController"></div>
    </div>
  </body>
</html>

JS

var app = angular.module("myApp", []);
app.controller("HomeController", ['$scope', '$log', function ($scope, $log) {
  $scope.title = "Some Title";
}]);

app.controller("ChildController", ['$scope', '$log', function ($scope, $log){
  $scope.$watch('$parent.title', function(newValue, oldValue) {
    $log.info('title was %s', oldValue);
    $log.info('title is now %s', newValue);
  });
}]);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! $scope.$watch('$parent.vm.title' .. did the trick. Could you please tell me what would be a more standardized way of structuring the controller JS?
Sure, the way I structured it above is what AngularJS has as structured controllers. Take a look at their documentation for controllers to get the full info on it docs.angularjs.org/guide/controller
Thanks helped me either. but the $watch is hitting twice. how can we over come with this.
0

Parent Controller :

$scope.cancelFlag = false;

Child Controller :

1) $parent.cancelFlag refers the parent controller.

2) If we make any changes in parent controller, watch will be invoked in child controller.

Example :

$scope.cancelFlag = true or false;

3) Here we can call child controller method by making changes in parent controller.

     $scope.$watch('$parent.cancelFlag', 
     function (newVal, oldVal)
     {
        if (newVal != null && newVal==true)
            $scope.childControllerMethod();
    });
    $scope.childControllerMethod= function () { };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.