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

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

 SearchF.get({}, function(data) {
     $scope.results = data;
 });

3 Comments

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.
@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?
Exactly. No need to put the code here, the problem was a scope one, see my own answer to the question.
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?

1 Comment

Yes something else is triggering the call, see my own answer to the question. Thanks a lot for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.