Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am trying to set a $scope.list variable in angular to be equal to a @users variable in my rails controller under the index method...any help?

ex: $scope.list = Users.query();


User_profile.rb (controller)

def index

@users = User.all 

end
share|improve this question
    
Just to make sure I understand you already have the $scope.list populated and want to set @Users, or the other way around? – Malkus Aug 2 '13 at 15:52
    
@Malkus users returns a rails query and I want to be able to set the $scope.list variable to the result of the query. However I want to do so without doing $scope.list = <%=raw users.to_json %>. But im not sure on how to do so with a $resource or with some other angular method – coderlyfe Aug 2 '13 at 15:57
up vote 0 down vote accepted

This is similar to other problems people have in regards to using JQuery $.ajax calls with AngularJS

If you take a look at this overview of Angular you will see Angular compiles everything into its own execution context. You need to bring your Rails call to angular in order for the data to appear in the $scope

The two options to resolve this are:

  1. Switch to the AngularJS $http service. This would be my preference: everything will already be in Angular and you could remove your dependendy on an additional library.
  2. Wrap your call in the $scope.$apply method to trigger a $digest loop when the Rails call is finished
share|improve this answer
    
Awesome thanks for the help! Is there anyway I could get a code sample to look at to help me get a better idea of what to do? – coderlyfe Aug 2 '13 at 17:02
    
Take a look at the angular API Reference It is a nice resource and has a lot of working examples. – Malkus Aug 2 '13 at 17:19
    
i am still struggling...maybe a quick example? – coderlyfe Aug 2 '13 at 17:43
    
Which solution where you going to go with? – Malkus Aug 2 '13 at 18:22
1  
ohhh i get it now thanks for the help! – coderlyfe Aug 2 '13 at 20:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.