0

Below is the code of my view and an abstract of code in my controller.

View:

<div id="search_results" ng-controller="SearchCtrl">
   <ul>
      <li ng-repeat="result in results">{{result.name}}</li>
   </ul>
</div>

Controller:

   myapp.controller('SearchCtrl', function($scope,SearchF) {    
      $scope.launch_search = function() {
         SearchF.update(function() {
            $scope.results = SearchF.get();
         });
      }
   })

The function .get() returns my data, but the view does not update. Looks like my scope ($scope.results) does not refer to the general scope. If I write the .update() block outside of the launch_search function, the view updates fine.

Any idea?

Thanks a lot everyone

siebmanb
  • 777
  • 1
  • 7
  • 15

3 Answers3

1

.get() is an async call, if it's using ngResource, assign the data in the callback:

 SearchF.get({}, function(data) {
     $scope.results = data;
 });
tymeJV
  • 103,943
  • 14
  • 161
  • 157
  • get() is not async, it just returns a data from a factory that was fetched by update() (which is async indeed). Check out my own answer. Thanks for helping. – siebmanb May 14 '14 at 16:04
  • @user1491411 -- So, `update` is async, and that stores data in the `SearchF` service - and the `get()` simply pulls the stored data? Can you post the `searchF` service? – tymeJV May 14 '14 at 16:04
  • Exactly. No need to put the code here, the problem was a scope one, see my own answer to the question. – siebmanb May 14 '14 at 16:49
1

The solution was to use $scope.$parent.results to reference the parent scope.

siebmanb
  • 777
  • 1
  • 7
  • 15
0

Off course some one has to trigger your function right ?

put a button with ng-click="launch_search()", on click of that your view updates. In case you want the view to be updated on load check out

How to execute angular controller function on page load?

Community
  • 1
  • 1
Subin Sebastian
  • 10,870
  • 3
  • 37
  • 42
  • Yes something else is triggering the call, see my own answer to the question. Thanks a lot for your help. – siebmanb May 14 '14 at 16:03