Javascript 如何在ng repeat angularjs中使用多重响应?
我在$scope.groups和$scope.profsmsgroups.groups两个多个作用域中得到了两个不同的响应Javascript 如何在ng repeat angularjs中使用多重响应?,javascript,angularjs,json,node.js,pug,Javascript,Angularjs,Json,Node.js,Pug,我在$scope.groups和$scope.profsmsgroups.groups两个多个作用域中得到了两个不同的响应 $http({ method: 'GET', url: '/api/getnewgroup' }) .then(function (response) { $scope.groups = response.data;
$http({
method: 'GET',
url: '/api/getnewgroup'
})
.then(function (response) {
$scope.groups = response.data;
}, function (response) {
window.location.href = '/';
});
$http({
method: 'GET',
url: '/api/getnewgroupforprofsms'
})
.then(function (response) {
$scope.profsmsgroups.groups = response.data;
}, function (response) {
window.location.href = '/';
});
我在$scope.profsmsgroups.groups范围中有groupname,因此td{{group.groupname}}显示正确
但我有$scope.groups中的contactsCount
那么如何使用多个ng重复响应呢
my$scope.profsms.groups json:
tr(ng-repeat='group in profsmsgroups.groups')
td
input(type = 'checkbox', ng-model='group.select')
td {{group.groupname}}
td {{group.contactsCount}}
我的$scope.groups响应
[
{
"id": 7,
"groupname": "Angular",
"createdAt": "2017-12-07T10:49:31.000Z",
"updatedAt": "2017-12-07T10:49:31.000Z",
"contactgroups": [
{
"id": 7,
"contact": {
"gsm": "4779306474"
}
}
]
},
{
"id": 3,
"groupname": "Vue",
"createdAt": "2017-12-05T09:36:15.000Z",
"updatedAt": "2017-12-05T09:36:15.000Z",
"contactgroups": []
},
{
"id": 5,
"groupname": "React",
"createdAt": "2017-12-05T09:36:29.000Z",
"updatedAt": "2017-12-07T10:46:28.000Z",
"contactgroups": []
},
{
"id": 6,
"groupname": "Node",
"createdAt": "2017-12-06T09:47:24.000Z",
"updatedAt": "2017-12-07T10:46:35.000Z",
"contactgroups": []
}
]
我假设您的注释id介于两者之间,您可以实现如下效果:-
var myApp=angular.module('myApp',[]);
控制器('MyCtrl',函数($scope,$timeout){
$scope.groups=[
{
“id”:3,
“组名”:“Vue”,
“ContactScont”:0,
“联系人组”:[]
},
{
“id”:5,
“组名”:“反应”,
“ContactScont”:0,
“联系人组”:[]
},
{
“id”:6,
“组名”:“节点”,
“ContactScont”:0,
“联系人组”:[]
},
{
“id”:7,
“groupname”:“Angular”,
“联系人”:1,
“联系人组”:[
{
“id”:7
}
]
}
];
$scope.pGroups=[
{
“id”:7,
“groupname”:“Angular”,
“createdAt”:“2017-12-07T10:49:31.000Z”,
“更新日期”:“2017-12-07T10:49:31.000Z”,
“联系人组”:[
{
“id”:7,
“联系人”:{
“gsm”:“4779306474”
}
}
]
},
{
“id”:3,
“组名”:“Vue”,
“createdAt”:“2017-12-05T09:36:15.000Z”,
“更新日期”:“2017-12-05T09:36:15.000Z”,
“联系人组”:[]
},
{
“id”:5,
“组名”:“反应”,
“createdAt”:“2017-12-05T09:36:29.000Z”,
“更新日期”:“2017-12-07T10:46:28.000Z”,
“联系人组”:[]
},
{
“id”:6,
“组名”:“节点”,
“createdAt”:“2017-12-06T09:47:24.000Z”,
“更新日期”:“2017-12-07T10:46:35.000Z”,
“联系人组”:[]
}
];
$scope.getContactCount=函数(组){
var match=$scope.groups.find(函数(d){返回d.id==group.id})
如果(匹配){
返回match.contactsCount
}
}
});代码>
名称
计数
{{g.groupname}}
{{getContactCount(g)}}
我认为您需要使用$index
作为组[$index]。contactsCount
,这听起来很简单,只是两个数组异步到达,我猜,要同步它们,您可能需要使用$q.all()
$index不工作,如何获得两个$scope.profsgroups.groups和$scope.groups,(如何在一次ng重复中获得两个作用域值?可能吗?@MohamedSameer能否为$scope.profsmsgroups和$scope.groupsgroups[$index]添加示例数据。contactsCount是的,很抱歉,已成功:)这是一个很好的方法吗?@MohamedSameer查看更新的ans,它可以很好地处理实际的问题data@MohamedSameer最好的方法是从后端发送自定义数据,这样您就不必发送两个请求。我不知道node.js:)bro,它工作正常:)但我在chrome控制台中遇到了以下错误:“TypeError:无法读取未定义的属性'find'”这意味着您的$scope.groups未定义
[
{
"id": 3,
"groupname": "Vue",
"contactsCount": 0,
"contactgroups": []
},
{
"id": 5,
"groupname": "React",
"contactsCount": 0,
"contactgroups": []
},
{
"id": 6,
"groupname": "Node",
"contactsCount": 0,
"contactgroups": []
},
{
"id": 7,
"groupname": "Angular",
"contactsCount": 1,
"contactgroups": [
{
"id": 7
}
]
}
]