[英]Send 2 json from server to angularjs controller by 1 request
In a rails project, I have 1 controller and 2 model. 在Rails项目中,我有1个控制器和2个模型。 I want send 1 request from angularjs
to rails server and for response, get 2 json array, 1. first model. 我想将1个请求从angularjs
发送到Rails服务器并进行响应,以获取2个json数组,第一个模型。 2. seccond model. 2.第二个模型。
Now I use below code, and get just 1 of 2 array: 现在,我使用下面的代码,并仅获得2个数组中的1个:
Rails Contorller: tables_controller.rb
: Rails tables_controller.rb
: tables_controller.rb
:
class Api::V1::TablesController < Api::V1::BaseController
def index
@table = Table.all
@mostagheltype = Mostagheltype.all
//I can just send 1 of model.
respond_with(Table.all)
end
end
Angularjs Controller: table.js
: Angularjs控制器: table.js
:
$scope.tables = Tables.index();
tableService.js
: tableService.js
:
'use strict';
var app = angular.module('tableService', ['ngResource']);
app.factory('Tables', function($resource) {
return $resource('/api/tables.json', {}, {
index: { method: 'GET', isArray: true}
});
});
I can push 2 table in 1 array in rails controller and then recieve this from angular controller, like below: 我可以在rails控制器中以1个数组推送2个表,然后从角度控制器中接收它,如下所示:
array = []
Table.all.each do |table|
array << { name: table.name, check: 1 }
end
Mostagheltype.all.each do |m|
array << { name: mostagheltype.name, check: 2}
end
//I can seprate 2 array by `check` value in angularjs part.
but I want a solution that I send each array separate. 但是我想要一个将每个数组单独发送的解决方案。 How can I do this? 我怎样才能做到这一点? Any idea? 任何想法?
Short answer no. 简短回答 You can't have two responses to one request. 您不能对一个请求有两个响应。 You can respond with an object that contains both arrays. 您可以使用包含两个数组的对象进行响应。
My is a little rusty. 我有点生锈。 So this may be more psuedo code than ruby; 因此,这可能比Ruby更像是伪代码;
obj = {}
obj.array1 =[]
obj.array2 =[]
Then populate each array and return the object. 然后填充每个数组并返回对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.