Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning restangular getList results to $scope

So it looks like in the examples you can do this:

App.controller('ProjectListCtrl', ['$scope', 'Restangular', function($scope, Restangular) {
  $scope.projects = Restangular.all('project/').getList();
}]);

But that doesn't work for me. When I ng-repeat project in projects and look at the scope, I see:

{ 
projects:  { 
  then: null
  catch: null
  finally: null
  call: null
  get: null
  restangularCollection: true
  push: null
 } 
}

Which looks like an un resolved promise object right?

This works fine but is more verbose:

lgtApp.controller('ProjectListCtrl', ['$scope', 'Restangular', function($scope, Restangular) {
  Restangular.all('project/').getList().then(function(projects){            
    $scope.projects = projects;                                             
  });                                                                       
}]);                                                                        

Did I miss something? This is in the docs:

$scope.owners = house.getList('owners')

Shouldn't matter, but this happens when I'm testing a phonegap app in the Ripple chrome plugin.

like image 481
Aaron McMillin Avatar asked Nov 21 '13 06:11

Aaron McMillin


3 Answers

$scope.projects = Restangular.all('project/').getList().$object;
like image 191
jla Avatar answered Nov 10 '22 02:11

jla


As an addition to the other comment, even though promise unwrapping was disabled, I implemented in Restangular a new way of having promises unwrap easily for you (Similar to what $resource does what without removing Promises). Check https://github.com/mgonto/restangular#using-values-directly-in-templates for more information :)

like image 45
mgonto Avatar answered Nov 10 '22 03:11

mgonto


As of angular 1.2, automatic unwrapping of promises was disabled and deprecated. So, while

$scope.projects = Restangular.all('project/').getList();

would have allowed you to access projects in your view directly in previous releases (for which the Restangular docs where likely written), that will no longer work automatically. You can reenable the feature via the $parseProvider.unwrapPromises() API, but it is deprecated, so your best bet is probably to manually resolve the promise in your controller like in your more verbose example.

See the checkin commit message for more details.

like image 7
kong Avatar answered Nov 10 '22 04:11

kong