-4

I have one $cope to be used in another controller. So I used $broadcast event to share my value. The method worked fine and I could assign the value to a $scope variable that is been $broadcast. But the issue comes over her. I could not able to bind or access the assigned value of the $scope outside the $on event.

Edit 1:

I have same set of event that is working fine in the same module. But this one doesn't.

app.controller('ctrl0',function($scope,$rootScope) {
    $rootScope.$broadcast($state.current.controller, { stateValue: $scope.data });
});

app.controller('ctrl1',function($scope,$rootScope) {
    $rootScope.$on($state.current.controller, function (event, args) {
        // This $scope binds in my view
        $scope.mylabel = args.data; 
    });

    $rootScope.$broadcast('myvariable', { search: $scope.searchvalue });
});

app.controller('ctrl2',function($scope,$rootScope) {
    $rootScope.$on('myvariable', function (event, args) {
        $scope.localscope = args.search;
        /* This prints the value in the console.
           This does not bind in my view. */
        console.log($scope.localscope);  
    });
    // This prints undefined in the console
    console.log($scope.localscope); 
});

I also tried by using $apply() function, but I didn't get the fruit.

Senthil Kumar J
  • 208
  • 2
  • 13
  • 2
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Tamas Hegedus Mar 22 '17 at 11:14
  • Do you event understand what `$rootScope.$on()` do? It listens for event to occur It won't do anything if event never occurs – Satpal Mar 22 '17 at 11:14
  • is your ' console.log($scope.localscope); // This prints undefined in the console' is consoled while loading controller on after the event is fired – deepakchauhan Mar 22 '17 at 11:16
  • Okay.. So when the event occurs, I would like to use that event value received in my controller. Isn't it possible? – Senthil Kumar J Mar 22 '17 at 11:17
  • @mean-elastic This will load after the event is fired. Because, I call this controller only when the broadcast is done. In the console order, the value comes first and undefined comes second. – Senthil Kumar J Mar 22 '17 at 11:21
  • You'll have to use that value from inside the event handler function. – Lekhnath Mar 22 '17 at 11:22

1 Answers1

0

The issue arises when you have the controller started, but have not declared the variable you are assigning in the $on, and it's value hasn't changed yet, so this would fix the 'error':

app.controller('ctrl2', function($scope, $rootScope) {
  $scope.localscope = null;
  $rootScope.$on('myvariable', function (event, args) {
    $scope.localscope = args.search;
    console.log($scope.localscope);  // This prints the value in the console
  });
  console.log($scope.localscope); // should print 'null'
});

So now when you print out the 2nd console.log it won't be undefined, it will be 'null'. If the event occurs now, it will still set that variable as you had imagined, and log it out to the console.

rrd
  • 5,789
  • 3
  • 28
  • 36
  • $on event triggers when, the controller gets initialized. Could that be any reason for the above method for showing null for the $scope? – Senthil Kumar J Mar 22 '17 at 12:17