1

While having multiple routed states in angularjs, each of the states are manipulated by separate controllers (in my case), for now i am using view model by this operator. Now i need to know if i have a property in a $scope variable which matches with the property of other state what happens to the scope of other state ?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Prabin Upreti
  • 498
  • 5
  • 16
  • Yes, when a view is destroyed and replaced with another, the old controller and its scope are destroyed. – georgeawg Jun 18 '17 at 16:14

1 Answers1

1

When instantiated controller creates a brand new scope object prototypically linked with parent $scope object. So if you have 2 separate sibling controllers their respective $scope objects are totally separate and have no connection.

The this when using controller with controllerAs syntax will attach properties onto $scope under the controllerAs specified key (if you specified vm in "ng-controller="MyCtrl as vm" then your this.someProp in controller will be pointing to the $scope.vm.someProp).

Thus technically it makes no difference whether you use this or $scope regarding your particular question.

If you want to have a shared state between the controllers the best approach is to have a service that will expose it, and you will get the state from both controllers injecting that service.

Karen Grigoryan
  • 5,234
  • 2
  • 21
  • 35