JavaScript AngularJS Resource not Resolved angularjs $resource resolve promise

I am new to AngularJS, so please be gentle.

I want to retrieve data from my backend using the $resource property of AngularJS, however it seems that the property is getting loaded before the actual call is completed.

Currently I have the following code:

Controller:

MyApp.controller('IndexCtrl', function IndexCtrl($scope, socket, carsRes) {
    console.log(carsRes.cars.get());
    $scope.cars = carsRes.cars.get();
});

Factory

.factory('carsRes', function($resource) {
        var result = {};
        result.cars = $resource('/cars/:id', {}, {
            'get': {method: 'GET'},
            'save': {method: 'POST'},
            'query': {method: 'GET', isArray: true},
            'remove': {method: 'DELETE'},
            'delete': {method: 'DELETE'}
        });

        return result;
 });

But at the point where I want to store carsRes.cars.get() in $scope.cars the call isn't completed yet, and a console.log tells me that the $resolved is still false.

How can I wait for the call to be resolved? I've read something about $q but it isn't really clear to me.

Answer:1

Im trying to post data back to a webmethod located in Default.aspx jquery code: data = "{'saveData':'testtestest'}" $.ajax({ type: "POST", contentType: "application/json; ...

Im trying to post data back to a webmethod located in Default.aspx jquery code: data = "{'saveData':'testtestest'}" $.ajax({ type: "POST", contentType: "application/json; ...

  1. ajax call always goes to error
  2. ajax call always
  3. ajax call always returns error
  4. ajax call always returns success
  5. ajax call always fails
  6. ajax call always error
  7. ajax call always function
  8. ajax request always error
  9. ajax request always
  10. ajax request always pending
  11. ajax request always fails
  12. jquery ajax call always returns error
  13. jquery ajax call always
  14. jquery ajax call always goes to error

to draw a rectangle in google map, I need to know northwest and southeast points to construct a LatLngbounds first. In my situation, I want to draw a rectangle with specific northwest point and a ...

to draw a rectangle in google map, I need to know northwest and southeast points to construct a LatLngbounds first. In my situation, I want to draw a rectangle with specific northwest point and a ...

  1. draw rectangle google maps
  2. draw rectangle google earth

I am extending Backbone views like: var baseView = Backbone.View.extend({ fruits : [], listFruits : function() { console.log(this.fruits); } }); var firstView = baseView.extend({ ...

I am extending Backbone views like: var baseView = Backbone.View.extend({ fruits : [], listFruits : function() { console.log(this.fruits); } }); var firstView = baseView.extend({ ...

  1. backbone view inheritance

I have a controller, simplified for brevity: .controller('myCtrl', ['$scope', 'data', function ($scope, data) { $scope.myVar = testFunction(data.name); function testFunction (name) { ... }; ...

I have a controller, simplified for brevity: .controller('myCtrl', ['$scope', 'data', function ($scope, data) { $scope.myVar = testFunction(data.name); function testFunction (name) { ... }; ...

  1. jasmine test scope variable
  2. angularjs test scope variable
  3. test-path variable scope
  4. junit test variable scope
  5. java test variable scope
  6. javascript test variable scope