View not updating after receiving a promise

1,814 views
Skip to first unread message

jsf

unread,
Jan 13, 2013, 10:47:28 PM1/13/13
to ang...@googlegroups.com
I'm using Angular 1.0.1. I have an Angular service which returns a promise. I'm calling this service from a controller. The process works in a sense that promise returns correct data but I cannot update the view with received data. Any thoughts on this problem?

// JS code in Controller
$scope.doSearch = function(query) {
    Search.google(query).then(
      function(result) {
        console.log("SUCCESS");
        $scope.gData = result;
      },
      function(result){
        console.log("ERROR");
      }
    );
  };

// View
<p>Data is {{ gData }}</p>

Joshua Miller

unread,
Jan 14, 2013, 2:33:43 AM1/14/13
to angular
Hello!

If the update of the data occurs outside of a digest cycle, you have to trigger one manually with $scope.$apply: http://docs.angularjs.org/api/ng.$rootScope.Scope. So in your case, you'll need to wrap the `$scope.gData = result` in a call to $apply.

Josh



--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
 
 

jsf

unread,
Jan 14, 2013, 9:12:26 AM1/14/13
to ang...@googlegroups.com
Thanks for the reply Josh. Yes, I tried to wrap `$scope.gData = result` in $scope.$apply(function() { $scope.gData = result; }); but then I get "$apply already in process" error message. 

Joshua Miller

unread,
Jan 14, 2013, 1:05:07 PM1/14/13
to angular

Can you post a plunker?

Josh

(sent from my phone)

jsf

unread,
Jan 14, 2013, 8:58:40 PM1/14/13
to ang...@googlegroups.com
I was able to resolve this problem. I had a wrong set-up (logically). Thanks for your time though.

Anthony Honaker

unread,
Feb 9, 2013, 11:13:25 AM2/9/13
to ang...@googlegroups.com
How did you resolve the problem?  I've run into the same scenario.

Anthony

Joshua Miller

unread,
Feb 9, 2013, 3:30:24 PM2/9/13
to angular
Hello! Can you post a Plunker or Fiddle?

Josh


To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.

To post to this group, send email to ang...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Anthony Honaker

unread,
Feb 9, 2013, 4:31:26 PM2/9/13
to ang...@googlegroups.com
Just like jsf, I resolved on my own.  I was having issue with nested callbacks and my promise wasn't surfacing appropriately.  

Thx.

Patrick Tisdel

unread,
Feb 26, 2015, 12:38:52 PM2/26/15
to ang...@googlegroups.com
Could people start posting their solutions when they find them? Honestly, there's nothing more frustrating than finding someone who has your problem--and fixed it--without explaining how.

lasjan

unread,
Dec 16, 2015, 10:41:24 AM12/16/15
to AngularJS
Hello,

I spent 2 hours struggling with this, so I joined to this group explicitly to make it easier for rest of developers. This "set up" error mentioned before is using ng-controller="your controller" in your view, and applying it again in your js code app.controller("your controller",..). It seems like duplicate, in my case removing  ng-controller="your controller" from view resolved this issue.

Regards
Reply all
Reply to author
Forward
0 new messages