0

function ParentCtrl($scope) {

//some function check whether data object in child scope is still null

}

function ChildCtrl($scope) {

$scope.data={};
$scope.func = function(){
  $scope.data.x = 1;

}; };

jsFiddle: http://jsfiddle.net/JHwxP/74/

9blue
  • 4,693
  • 9
  • 29
  • 43
  • You cannot access child scopes from a parent scope.http://stackoverflow.com/questions/13428042/angularjs-access-to-child-scope – geniuscarrier Oct 01 '13 at 22:56
  • Define the property on parent scope and access it from child scopes. Scopes inherit prototypical. See this: http://jsfiddle.net/yjVD9/1/ Very helpful! – 9blue Oct 01 '13 at 23:56

1 Answers1

0

You can use a system of events like you can see here : http://jsfiddle.net/patxy/RAVFM/

If you have 2 controllers with a shared service, you can do it that way :

var myModule = angular.module('myModule', []);
myModule.factory('mySharedService', function($rootScope) {
  var sharedService = {};

  sharedService.message = '';

  sharedService.prepForBroadcast = function(msg) {
    this.message = msg;
    this.broadcastItem();
  };

  sharedService.broadcastItem = function() {
    $rootScope.$broadcast('handleBroadcast');
  };

  return sharedService;
});

function ControllerZero($scope, sharedService) {
  $scope.handleClick = function(msg) {
    sharedService.prepForBroadcast(msg);
  };

  $scope.$on('handleBroadcast', function() {
    $scope.message = sharedService.message;
  });        
}

function ControllerOne($scope, sharedService) {
  $scope.$on('handleBroadcast', function() {
    $scope.message = 'ONE: ' + sharedService.message;
  });        
}

function ControllerTwo($scope, sharedService) {
  $scope.$on('handleBroadcast', function() {
    $scope.message = 'TWO: ' + sharedService.message;
  });
}

ControllerZero.$inject = ['$scope', 'mySharedService'];        

ControllerOne.$inject = ['$scope', 'mySharedService'];

ControllerTwo.$inject = ['$scope', 'mySharedService'];
Deblaton Jean-Philippe
  • 11,188
  • 3
  • 49
  • 66