如何从Angular获取/复制/访问Rails中的数组

时间:2021-05-21 15:06:55

How can i get/copy/access the array that is in Rails from Angular? I have an array of strings, called @array_of_names, in my def new function and would like to have the same array (@array_of_names) in my Angular function. What's the way to do that so i could have @array_of_names in my Angular side?

如何从Angular获取/复制/访问Rails中的数组?我的def函数中有一个名为@array_of_names的字符串数组,并希望在我的Angular函数中使用相同的数组(@array_of_names)。有什么方法可以让我在Angular方面拥有@array_of_names?

Rails file

def new 
  @account = Account.new
  @array_of_names = read_names()
end

Angular file

(() => {

  angular
    .module('accounts')
    .controller('AccountsController', AccountsController)

  AccountsController.$inject = []

  //testing function
  function AccountsController () {
    let vm = this;
    vm.claim = "Hello, world!";
  }

})()

1 个解决方案

#1


0  

You can accomplish this via AJAX. You need to set up a route in the Rails app that returns a JSON object for Angular to use.

你可以通过AJAX实现这一目标。您需要在Rails应用程序中设置一个路由,该路由返回一个JSON对象供Angular使用。

Rails
Replace controller_name with your controller name

Controller (/app/controllers/controller_name.rb):

Rails将controller_name替换为您的控制器名称Controller(/app/controllers/controller_name.rb):

def get_names
  render json: read_names()
end

Routes (/config/routes.rb):

get '/get_names' => 'controller_name#get_names'

Angular

function AccountsController($http){

  var vm = this;

  $http.get('/get_names').then(function(response){
    vm.names = response.data;
  });
}

AccountsController.$inject = ['$http'];

See this link to really understand what angular is doing: https://docs.angularjs.org/api/ng/service/$http

请参阅此链接以了解角度正在做什么:https://docs.angularjs.org/api/ng/service/$http

Here is a link explaining how to work with Rails and more complex JSON responses https://www.leighhalliday.com/responding-with-json-in-rails

这是一个解释如何使用Rails和更复杂的JSON响应的链接https://www.leighhalliday.com/responding-with-json-in-rails

#1


0  

You can accomplish this via AJAX. You need to set up a route in the Rails app that returns a JSON object for Angular to use.

你可以通过AJAX实现这一目标。您需要在Rails应用程序中设置一个路由,该路由返回一个JSON对象供Angular使用。

Rails
Replace controller_name with your controller name

Controller (/app/controllers/controller_name.rb):

Rails将controller_name替换为您的控制器名称Controller(/app/controllers/controller_name.rb):

def get_names
  render json: read_names()
end

Routes (/config/routes.rb):

get '/get_names' => 'controller_name#get_names'

Angular

function AccountsController($http){

  var vm = this;

  $http.get('/get_names').then(function(response){
    vm.names = response.data;
  });
}

AccountsController.$inject = ['$http'];

See this link to really understand what angular is doing: https://docs.angularjs.org/api/ng/service/$http

请参阅此链接以了解角度正在做什么:https://docs.angularjs.org/api/ng/service/$http

Here is a link explaining how to work with Rails and more complex JSON responses https://www.leighhalliday.com/responding-with-json-in-rails

这是一个解释如何使用Rails和更复杂的JSON响应的链接https://www.leighhalliday.com/responding-with-json-in-rails