JavaScript Sorting an array that is the result of a $resource call in AngularJS

I'm trying to get the results of an API call and save it to $scope.segments, so that later I can sort the array using $scope.segments.sort()

However, since $scope.segments = SegmentsService.getSegments(jobId); does an async call, $scope.segments is undefined and the sorting never works.

Struggling with this the full day. How can I resolve this?

Here's my controller:

angular.module('appApp')
  .controller('MainCtrl', function ($scope, $routeParams, $filter, JobsFactory, SegmentsFactory, SegmentsService) {

    var jobId = $routeParams.id;

    // gets the segments from the API
    $scope.segments = SegmentsService.getSegments(jobId);

    // returns 'undefined', because $resource has not populated the 'segments' array yet
    console.log($scope.segments);

    // returns "TypeError: Cannot read property 'sort' of undefined", because $scope.segment doesn't exist (yet)
    $scope.segments.sort(function(a, b) {
      return a.sequence - b.sequence;
    });

  });

Here's my service:

angular.module('appApp')
  .service('SegmentsService', function (SegmentsFactory) {

    var segments = [];

    this.getSegments = function(jobId) {
      SegmentsFactory.query({ job_id: jobId }).$promise.then(function(data) {
        segments = data;
        return segments;
      }, function(err) {
        //fail
      });
    };

  });

And last, here's my factory:

angular.module('appApp')
  .factory('SegmentsFactory', function ($resource) {
    return $resource('http://localhost:3000/api/v1/segments/:id');
  });
Answer:1

I would like to handle a click event on a node with dracula JS, the node is added using this code : g.addNode(capability, {render:render}); Any suggestion please ?

I would like to handle a click event on a node with dracula JS, the node is added using this code : g.addNode(capability, {render:render}); Any suggestion please ?

What would be the equivalent of this without using a for loop? Unsure what array method to use function func(x){ var y = []; for(i=0;i<x.length;i++){ y.push(x[i]); y.push(...

What would be the equivalent of this without using a for loop? Unsure what array method to use function func(x){ var y = []; for(i=0;i<x.length;i++){ y.push(x[i]); y.push(...

I have a MongoDB collection called 'Prices' that I am trying to query for prices between a 'startDate' and 'endDate'. The collection stores prices every 10 seconds, however when querying this ...

I have a MongoDB collection called 'Prices' that I am trying to query for prices between a 'startDate' and 'endDate'. The collection stores prices every 10 seconds, however when querying this ...

  1. mongodb query large data
  2. mongodb query larger than
  3. mongodb large query optimization
  4. mongodb large query

I'm doing something like: $.ajax({ type: 'POST', dataType: 'json', contentType: 'application/json', url: '/Modal/getModalInfo', data: '{"machine":"' + machine + '"}', success:...

I'm doing something like: $.ajax({ type: 'POST', dataType: 'json', contentType: 'application/json', url: '/Modal/getModalInfo', data: '{"machine":"' + machine + '"}', success:...

  1. ajax call returning object object
  2. ajax call returning empty response
  3. ajax call returning html page
  4. ajax call returning html instead of json
  5. ajax call returning error
  6. ajax call returning undefined
  7. ajax call returning 302
  8. ajax call returning 404
  9. ajax call returning html
  10. ajax call returning json data
  11. ajax call returning 500 error
  12. ajax request returning full page code
  13. ajax request returning undefined
  14. ajax request returning 404
  15. ajax request returning object
  16. ajax call not returning success or error
  17. ajax call not returning response
  18. ajax call not returning data
  19. ajax call not returning to success
  20. ajax call not returning view