gpt4 book ai didi

javascript - 如何在 ng-repeat angularjs 中使用多重响应?

转载 作者:行者123 更新时间:2023-11-29 23:29:57 24 4
gpt4 key购买 nike

$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.groups 和 $scope.profsmsgroups.groups 两个多重作用域中得到了两个不同的响应。

tr(ng-repeat='group in profsmsgroups.groups')
td
input(type = 'checkbox', ng-model='group.select')
td {{group.groupname}}
td {{group.contactsCount}}

我在 $scope.profsmsgroups.groups 范围内有组名,所以 td {{group.groupname}} 正确显示

但我在 $scope.groups 中有 contactsCount

那么如何使用多次ng-repeat响应呢?

我的 $scope.profsms.groups json:

[
{
"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": []
}
]

我的 $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
}
]
}
]

最佳答案

我假设你在两者之间有评论 ID 而不是你可以像这样实现它:-

var myApp = angular.module('myApp',[]);
myApp.controller('MyCtrl',function($scope, $timeout) {
$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
}
]
}
];
$scope.pGroups=[
{
"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": []
}
];
$scope.getContactCount=function(group){
var match=$scope.groups.find(function(d){return d.id==group.id})
if(match){
return match.contactsCount
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
<table style="width:100%">
<tr>
<th>name</th>
<th>count</th>
</tr>
<tr ng-repeat="g in pGroups">
<td style="text-align: center;">{{g.groupname}}</td>
<td style="text-align: center;">{{getContactCount(g)}}</td>
</tr>
</table>
</div>

关于javascript - 如何在 ng-repeat angularjs 中使用多重响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47692641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com