Skip to content Skip to sidebar Skip to footer

Sharing Data Between Different Instances Of The Same Controller (ng-controller)

I'm new to angular and am trying to accomplish a very simple task, which doesn't seem to be that simple in angular. I am having some data (just a variable named var1 for simplicity

Solution 1:

OK, this is related to Miško's saying:

"..if you use ng-model there has to be a dot somewhere. If you don't have a dot, you're doing it wrong.."

In short, when you are initializing var1 in the second controller, you are creating your own scope (even when you're using $rootScope).

Instead of accessing $rootScope directly:

$rootScop.var1 = 1;

Try to use an object:

$rootScope.dataObj = {var1: 1};

https://plnkr.co/edit/oVB16H0PBLFx3PyLhuRN?p=preview

For further reading:

Why don't the AngularJS docs use a dot in the model directive?

http://jimhoskins.com/2012/12/14/nested-scopes-in-angularjs.html

https://thinkster.io/egghead/the-dot

Post a Comment for "Sharing Data Between Different Instances Of The Same Controller (ng-controller)"