$scope usage


#1
<div ng-controller="CtrlOne">
{{VARIABLE_ONE}}
</div>

<div ng-controller="CtrlTwo">
{{VARIABLE_TWO}}
</div>

.controller("CtrlOne", function($scope){
$scope.VARIABLE_ONE = "ONE";
})

.controller("CtrlTwo", function($scope){
$scope.VARIABLE_ONE = "1";
$scope.VARIABLE_TWO = "2";
})

How can i set $scope.VARIABLE_ONE = “1” from CtrlTwo ?? The above code is not working. Any help will be appreciated.


#2

I think that VARIABLE_ONE must be a global variable or you pass variable to controller.


#3

you have few solutions, eg:

  1. use $rootScope
  2. add third controller which will create parent scope for existing
  3. use events to communication between controllers

details how you can do it, you find here:
https://docs.angularjs.org/guide/scope


#4

Thank you guys. $rootScope solved my problem.


#5

Hi, Could you put how solved?


#6
<div ng-controller="CtrlOne">
{{VARIABLE_ONE}}
</div>

<div ng-controller="CtrlTwo">
{{VARIABLE_TWO}}
</div>

.controller("CtrlOne", function($scope){
$scope.VARIABLE_ONE = "ONE";
})

.controller("CtrlTwo", function($scope, **$rootScope**){
**$rootScope.VARIABLE_ONE = "1";**
$scope.VARIABLE_TWO = "2";
})