Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><a href="http://jsfiddle.net/VP4d5/2/" rel="noreferrer">http://jsfiddle.net/VP4d5/2/</a></p> <p>Here's an updated fiddle. Basically if you're going to share the same data object between two controllers from a service you just need to use an object of some sort aside from a string or javascript primitive. In this case I'm just using a regular Object {} to share the data between the two controllers.</p> <p>The JS</p> <pre><code>angular.module('myApp', []) .controller('Ctrl1', function ($scope, App) { $scope.localData1 = App.data; }) .controller('Ctrl2', function ($scope, App) { $scope.localData2 = App.data; }) .service('App', function () { this.data = {status:'Good'}; }); </code></pre> <p>The HTML</p> <pre><code>&lt;div ng-controller="Ctrl1"&gt; &lt;div&gt; Ctrl1 Status is: {{status}} &lt;/div&gt; &lt;div&gt; &lt;input type="text" ng-model="localData1.status" /&gt; &lt;/div&gt; &lt;div ng-controller="Ctrl2"&gt;Ctrl2 Status is: {{status}} &lt;div&gt; &lt;input type="text" ng-model="localData2.status" /&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>Nothing wrong with using a service here but if the only purpose is to have a shared object across the app then I think using .value makes a bit more sense. If this service will have functions for interacting with endpoints and the data be sure to use angular.copy to update the object properties instead of using = which will replace the service's local reference but won't be reflected in the controllers.</p> <p><a href="http://jsfiddle.net/VP4d5/3/" rel="noreferrer">http://jsfiddle.net/VP4d5/3/</a></p> <p>The modified JS using .value</p> <pre><code>angular.module('myApp', []) .controller('Ctrl1', function ($scope, sharedObject) { $scope.localData1 = sharedObject; }) .controller('Ctrl2', function ($scope, sharedObject) { $scope.localData2 = sharedObject; }) .value("sharedObject", {status:'Awesome'}); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload