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 8654411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:46:53+00:00 2026-06-12T14:46:53+00:00

Note: I also posted this question on the AngularJS mailing list here: https://groups.google.com/forum/#!topic/angular/UC8_pZsdn2U Hi

  • 0

Note: I also posted this question on the AngularJS mailing list here: https://groups.google.com/forum/#!topic/angular/UC8_pZsdn2U

Hi All,

I’m building my first AngularJS app and am not very familiar with Javascript to begin with so any guidance will be much appreciated 🙂

My App has two controllers, ClientController and CountryController. In CountryController, I’m retrieving a list of countries from a CountryService that uses the $resource object. This works fine, but I want to be able to share the list of countries with the ClientController. After some research, I read that I should use the CountryService to store the data and inject that service into both controllers.

This was the code I had before:

CountryService:

services.factory('CountryService', function($resource) {
    return $resource('http://localhost:port/restwrapper/client.json', {port: ':8080'});
});

CountryController:

//Get list of countries         
//inherently async query using deferred promise
$scope.countries = CountryService.query(function(result){       
        //preselected first entry as default
    $scope.selected.country = $scope.countries[0];  
});

And after my changes, they look like this:

CountryService:

services.factory('CountryService', function($resource) {
    var countryService = {};

    var data;
    var resource = $resource('http://localhost:port/restwrapper/country.json', {port: ':8080'});

    var countries = function() {
        data = resource.query();
        return data;
    }

    return {
        getCountries: function() {
            if(data) {
                console.log("returning cached data");
                return data;
            } else {
                console.log("getting countries from server");
                return countries(); 
            }

        }
    };
  });

CountryController:

$scope.countries = CountryService.getCountries(function(result){
        console.log("i need a callback function here...");
});

The problem is that I used to be able to use the callback function in $resource.query() to preselect a default selection, but now that I’ve moved the query() call to within my CountryService, I seemed to have lost what.

What’s the best way to go about solving this problem?

Thanks for your help,
Shaun

  • 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-12T14:46:54+00:00Added an answer on June 12, 2026 at 2:46 pm

    Ok..so looks like I solved the problem by passing a callback function all the way up to the resource.query() call. Still not sure if this is the best way to do this.

    For reference, this is what I did:

    CountryController:

    $scope.countries = CountryService.getCountries(function(){
        //preselected default
        $scope.selected.country = $scope.countries[0];  
    });
    

    CountryService:

    //Country Service. Will contain all relevant rest methods here
    services.factory('CountryService', function($resource) {
        var countryService = {};
    
        var data;
        var resource = $resource('http://localhost:port/restwrapper/country.json', {port: ':8080'});
    
        var countries = function(callback) {
            data = resource.query(callback);
            return data;
        }
    
    
        return {
            getCountries: function(callback) {
                if(data) {
                    console.log("returning cached data");
                    return data;
                } else {
                    console.log("getting countries from server");
                    return countries(callback); 
                }
    
            }
        };
      });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note: This question is also posted on the WiX Users mailing list . I
Note: I've cross-posted this question in the grails-user mailing list This weekend using this
I've written up a minimal example of this. The code is posted here: https://gist.github.com/1524725
NOTE: This is a followup to my question here. I have a program that
NOTE: This is an old question and the answers here no longer works (since
The Django docs say this on the subject: Note also that Django stores signal
How would you do this PHP switch statement? Also note that these are much
Note, this is not a duplicate of .prop() vs .attr() ; that question refers
(Note: This is not a question about what is the best way with code
I've based my work on Phil Haack's article here: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx I've also looked over

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.