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

  • Home
  • SEARCH
  • 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 8411961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:33:27+00:00 2026-06-10T00:33:27+00:00

This is a follow up to my previous AngularJS question . I am trying

  • 0

This is a follow up to my previous AngularJS question.

I am trying to replicate the functionality of the $resource service that is mentioned in this video tutorial: link.

In this tutorial, it is shown that you can update a scope variable and its view using an asynchronous call. First, here’s the resource that’s set up:

$scope.twitter = $resource('http://twitter.com/:action',
    {action: 'search.json', q: 'angularjs', callback: 'JSON_CALLBACK'},
    {get: {method: 'JSONP'}});

Then the get function is called on the resource to make the ajax call and update the necessary scope variable:

$scope.twitterResult = $scope.twitter.get();

What’s happening here is that $scope.twitter.get() immediately returns an empty object reference. Then, when the ajax request returns, that object is updated with the data from Twitter. The view then updates all {{twitterResult}} instances and you see the data that was returned.

What I don’t get is, when the object is updated in the asynchronous callback, how does the scope know that the value has changed?

I don’t think it does – the callback must call some kind of update function on the scope, right? But if so, how is this done? I have heard of a function called $apply that might be the answer – but how do you reference $scope.$apply() from inside the resource?

I have created a JSFiddle to illustrate the problem: http://jsfiddle.net/Qcz5Y/10/

(Edit: Here is a newer JSFiddle with a different async call to illustrate the problem using an ajax call instead of setTimeout: http://jsfiddle.net/Qcz5Y/14/)

In this example, you will see a controller and a resource. The controller’s scope has a property, fruit, which starts out set to an object. When you click ‘Get new fruit’, that property is set to fruitResource.get(), which immediately returns an empty object. It then makes an asynchronous call, and finally executes a callback which updates the value of the object that was originally returned.

When this happens, the value of $scope.fruit is properly updated to what was set in the callback. But it isn’t reflected in the view.

If you click “Print current fruit”, it will log to the console the current value of $scope.fruit. This was just for checking; it has the side-effect of updating the view.

I feel like I need to be doing something like $scope.$apply() at the bottom of the get_callback function (you’ll see it in the code). Would that be the right thing to do? If so, how do I get a reference to $scope inside the fruitResource? I know I can probably pass $scope into fruitResource.get() as a parameter and then reference it that way, but the $resource.get() function in the example mentioned at the top doesn’t need to do this, so I was hoping that AngularJS provides some way to get the scope from services automatically.

Thanks!

  • 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-10T00:33:29+00:00Added an answer on June 10, 2026 at 12:33 am

    I figured out that you can pass $rootScope into a service and then call $apply on that. So when you want to update your view, you just do $rootScope.$apply().

    Here is the updated JSFiddle: http://jsfiddle.net/Qcz5Y/11/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So this is a follow-up to a previous question that I asked: Trying to
This is a follow up to my previous question . Consider that I write
Note that this is a follow-up question from my previous one: How to memorize
This is a follow up question to my previous question . I am trying
This is a follow up to a previous question that I had before about
This is a follow up question to my previous question . I am trying
This is a follow-up question of my previous post . I am trying to
This is a follow up to a previous question. I'm trying to convert some
This is a follow up from my previous question I have this code basically
This is a follow-up to my previous question . Suppose I am writing the

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.