Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9065795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:35:48+00:00 2026-06-16T16:35:48+00:00

Everything works fine but I have problem getting source from service. I have SERVICE

  • 0

Everything works fine but I have problem getting source from service.

I have SERVICE code below.

app.factory('myService', 
    function($rootScope, ResourceData) {
    var result = {};
    var data   = ResourceData.query();

    result.getData = function() {
        // problem here
    }

    return result;
});

And CONTROLLER contain code.

app.controller('myController', ['$scope', 'myService', 
    function myController($scope, myService) {
       $scope.data = myService.getData();
});

My problem is if I have function in my SERVICE like this

result.getData = function() {
   return data;
}

Everything works fine but I need to filter that data before I get it

If I change body like this I get an empty array the problem seems like it is from AngularJS.

If I create static array it works.

result.getData = function() {
   var arr = [];

        angular.forEach(data, function(item, key) {
              // simple filter
              if(item.ID > 10) {
                  return;
              }          
              else {
                  arr.push(item);
              }  
        });

        return arr;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:35:49+00:00Added an answer on June 16, 2026 at 4:35 pm

    The result of “ResourceData.query()” is asynchronous:

    When the data is returned from the server then the object is an instance of the resource class… It is important to realize that invoking a $resource object method immediately returns an empty reference (object or array depending on isArray). Once the data is returned from the server the existing reference is populated with the actual data.

    When the page first loads, I’m guessing that the controller runs before the data is returned from the server, so the first time getData() is called, data is probably not yet populated with any data, so you get an empty array.

    One solution to your problem would be to filter the data in the view/HTML with an Angular filter, rather than in the service.

    <div ng-repeat="item in data | filter:myFilter">
       {{item.ID}}: {{item...}}
    </div>
    

    Then in your controller:

    $scope.myFilter = function(item) {
       return item.ID <= 10;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing Swing application, and everything works fine usually. But I have an GUI
I have my pickerview set up and everything works fine, but right now I
I have some miniapp that use delayed_job. On my localhost everything works fine, but
I'm getting a 404 on my server (IIS 7) but everything works fine on
I'm using DataContractSerializer to serialize/deserialize my classes to/from XML. Everything works fine, but at
I have a simpleAsp.net page which I make it Ajaxable. everything works fine but
We are trying to switch to gradle. Everything works fine but i have a
I'm using Spreadsheet::WriteExcel packet to create some Excel files. Everything works fine but when
Trying out the example usage of codebrew rails-backbone , everything works fine but the
I started using Zend_Paginator, it works everything fine but I noticed that there is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.