在AngularJS中访问Rails实例变量 [英] Accessing Rails instance variable in AngularJS

查看:41
本文介绍了在AngularJS中访问Rails实例变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在index方法下,在我的rails控制器中将一个$ scope.list变量设置为angular,使其等于一个 @users 变量...有帮助吗?

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

推荐答案

这类似于人们在将AngularJS与JQuery $.ajax 调用一起使用时遇到的其他问题

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

如果您查看此Angular概述,您将看到Angular将所有内容编译到其自己的执行上下文.您需要使Rails调用角度化,以便数据出现在 $ scope

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

解决此问题的两个选项是:

  1. 切换到AngularJS $ http 服务.这是我的偏好:一切都已经在Angular中了,您可以删除对其他库的依赖.
  2. 在Rails调用完成后,将您的调用包裹在 $ scope.$ apply 方法中,以触发 $ digest 循环
  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

这篇关于在AngularJS中访问Rails实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆