title: Best (idiomatic) way to refresh data from a service in AngularJS
body: <p>I am facing a "problem" with AngularJS, services and scope. </p>
  
  <p>It is not really a problem (I found a couple of ways to make it work), but I would like to know if I am doing the right thing, or if what I am doing can lead to problems in the future</p>
  
  <p>I have a service that holds some global data; the service has two methods:
  getData()
  refreshData()</p>
  
  <p>refreshData triggers some work (rest invocation etc.) and it is called at precise points inside different controllers, in response to user actions (button clicks etc).</p>
  
  <p>getData is (obviously) called to get the data back.</p>
  
  <p>In the controllers, how should I use it to access the data and put it in scope, so that it can be accessed from the view(s)?</p>
  
  <p>Alternative 1:</p>
  
  <pre><code>controller('MyController', function ($scope, myService) {   
       $scope.data = myService.getData();
       //use data in functions and in the view, ex: ng-hide="data.forbidden"
  </code></pre>
  
  <p>Alternative 2:</p>
  
  <pre><code>controller('MyController', function ($scope, myService) {   
       $scope.data = function() { return myService.getData(); }
       //use data() in functions and in the view, ex: ng-hide="data().forbidden"
  </code></pre>
  
  <p>Alternative 3:</p>
  
  <pre><code>controller('MyController', function ($scope, myService) {   
       $scope.forbidden = function() { return myService.getData().forbidden; }
       //... one function for each data.member used in this view
       //usage in the view: ng-hide="forbidden()"
  </code></pre>
  
  <p>Alternative 4: use $apply or $watch</p>
  
  <p>I am currently using the second approach, as it works even when a new controller is not created (think about different partials in the same page, with different controllers).</p>
  
  <p>Does it make any sense? Or is there a better approach?</p>