Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use angular resource sails?

i am using angular resource sails.

var items = sailsResource('roles').query(); // GET /item
$scope.roles = items;
angular.forEach($scope.roles, function(value, key) {
    console.log(key + ': ' + value);
});

output: undefined.

How to parse this query?

like image 647
Angu Avatar asked Jun 17 '15 05:06

Angu


2 Answers

Check this part of the documentation: https://github.com/angular-resource-sails/angular-resource-sails#success-and-error-callbacks

If you want to access the data you fetched, you'll probably have to provide the query function with callbacks. So your code would become

sailsResource('roles').query(function(items) { // GET /item
    $scope.roles = items;
    angular.forEach($scope.roles, function(value, key) {
        console.log(key + ': ' + value);
    });
});
like image 121
Fissio Avatar answered Oct 22 '22 19:10

Fissio


The query method is asynchronous. sailsResource creates $resource API compatible services so you have to do your looping in a callback function.

For example

$scope.roles = sailsResource('roles').query(function(roles) {
    angular.forEach(roles, function(value, key) {
        // and so on
    });
});

You can also use the $promise property to access the promise, eg

$scope.roles = sailsResource('roles').query();

$scope.roles.$promise.then(function() {
    angular.forEach($scope.roles, function(value, key) {
        // etc
    });
});
like image 22
Phil Avatar answered Oct 22 '22 20:10

Phil