AngularJS中的多查询
我需要用AngularJS开发一个页面 我有两张桌子AngularJS中的多查询,angularjs,Angularjs,我需要用AngularJS开发一个页面 我有两张桌子 Table 1 : StateList StateCode | StateName ------------------------------- AZ | ARIZONA CA | CALIFORNIA NY | NEW YORK Table 2: CustomerDetail Name | BornState | LivingState ----
Table 1 : StateList
StateCode | StateName
-------------------------------
AZ | ARIZONA
CA | CALIFORNIA
NY | NEW YORK
Table 2: CustomerDetail
Name | BornState | LivingState
-----------------------------------------
Peter | AZ | NY
Bob | CA | AZ
希望在HTML页面上显示如下列表
Customer Name | Customer BornState | Customer LivingState
------------------------------------------------------------------------
Peter | ARIZONA | New York
Bob | CALIFORNIA | ARIZONA
我希望显示每种状态信息的状态名称,而不是状态代码,例如BornState和LivingState。
我可以从表2中获取列表:CustomerDetail,但如何将状态代码与状态名称转换为每种类型的状态信息?
提前谢谢
添加更多细节
我有$stateProvider,如下所示
.state("customerDetail",{
url:"/customer/detail/:customerId",
templateUrl:"app/customer/customerDetail.html",
controller: "DetailCtrl as vm",
resolve: {
customer: function (Restangular, $stateParams) {
return Restangular.one('customer', $stateParams.customerId).get();
}
}
})
这给我一个数组
{"customerId":"1","customerName":"Bob","BornState":"CA","LivingState":"AZ"}
我的状态数组列表如下
[{"Code":"NY","Name":"New York"},{"Code":"AZ","Name":"ARIZONA"},{"Code":"CA","Name":"CALIFORNIA"}]
我想显示完整的状态名称而不是状态代码您确实应该使用SQL来实现这一点,并将两个表连接起来,但如果您想要角度解决方案,这就是: 在控制器中:
$scope.cus = [array of customers];
var countries = [array of countries];
$scope.countries = {};
$.each(countries, function(k,v) {
$scope.countries[v.StateCode] = v.StateName;
});
和您的HTML表格:
<tr ng-repeat="row in cus">
<td>{{row.name}}</td>
<td>{{countries[row.BornState]}}</td>
<td>{{countries[row.LivingState]}}</td>
</tr>
{{row.name}
{{国家[row.BornState]}
{{国家[row.LivingState]}
我所做的就是创建一个新对象作为哈希表,在这个哈希表中,抓取取取O(1)。这是一个plunkr
//编辑
在您的范围内:
$scope.StateList = {} ;
//init the $scope.StateList
var statList = [{"Code":"NY","Name":"New York"}, {"Code":"AZ","Name":"ARIZONA"},{"Code":"CA","Name":"CALIFORNIA"}];
for(var i in statList){
$scope.StateList[statList[i]["Code"]] = statList[i]["Name"];
}
$scope.CustomerDetail = [{
"CustomerName" : "Peter",
"CustomerBornState" :"AZ",
"CustomerLivingState" : "NY"
},{
"CustomerName" : "Peter",
"CustomerBornState" :"CA",
"CustomerLivingState" : "AZ"
}];
以及html:
<table>
<tr ng-repeat="customer in CustomerDetail">
<td>
{{customer.CustomerName}}
</td>
<td>
{{StateList[customer.CustomerBornState]}}
</td>
<td>
{{StateList[customer.CustomerLivingState]}}
</td>
</tr>
{{customer.CustomerName}
{{StateList[customer.CustomerBornState]}
{{StateList[customer.CustomerLivingState]}
你也可以用更干净的方式来做。在控制器中,从customerDetail stateProvider获取数据后,在$scope.customerDetail中添加一个具有真实城市名称的字段,并将其显示在视图中。我建议您熟悉。。我已经将越来越多的特定于应用程序的模型交互/增强转移到客户端
这可以通过Join语句快速、轻松地完成。您可能希望共享一些代码供人们查看。添加在上述说明中。谢谢你的帮助,如果不行就告诉我。如果它起作用,你可以接受这个答案。它不起作用我的状态列表就像[{“代码”:“NY”,“名称”:“纽约”},{“代码”:“AZ”,“名称”:“亚利桑那”},{“代码”:“CA”,“名称”:“加利福尼亚”}]