Sorry for the bad title, couldn't really think of an apropriate one.
不好意思,不好意思,我想不出一个合适的名字。
Anyways, I have an array of objects, which are all listed in a ng-repeat. Then, I want to find the object in another array of objects, which has the same id as the one above and get another of its properties.
总之,我有一个对象数组,它们都列在一个ng-repeat中。然后,我想要在另一个对象数组中找到对象,该对象具有与上面那个相同的id,并获得另一个属性。
I hope this example code will simplify things:
我希望这个示例代码可以简化以下内容:
$scope.discussions = [
{
recipient: {
id: 'theid'
},
messages: []
}
];
$scope.friends = [
{
id: 'theid',
online: false
}
];
And in my view I have:
在我看来
<div ng-repeat="discussion in discussions">
<div>Online = {{//code here}}</div>
</div>
Tried both
都试过
ng-class="{online: {{friends.some(fr => fr.id == discussion.recipient._id).online}}; }"
and
和
ng-class="{online: friends.some(fr => fr.id == discussion.recipient._id).online }"
1 个解决方案
#1
2
My suggestion is to define a function within your angular controller that can match a single discussion
to the matching element in your $scope.friends
. That function might look like this (this can be improved, but it gets the job done):
我的建议是在您的角度控制器中定义一个函数,该函数可以将单个讨论与$scope.friends中的匹配元素匹配。这个函数可能看起来像这样(可以改进,但它可以完成工作):
$scope.findMatchingId = function (discussion) {
var matchingFriend;
$scope.friends.some(function (friend) {
if (friend.id === discussion.recipient.id) {
matchingFriend = friend;
return true;
}
return false;
});
return matchingFriend;
};
Then in your html, you can just call that function on the element within the ng-repeat like this:
然后在html中,只需调用ng-repeat元素中的函数即可:
<div ng-repeat="discussion in discussions">
<div>Online = {{findMatchingId(discussion)}}</div>
</div>
See this plnkr
看到这个plnkr
#1
2
My suggestion is to define a function within your angular controller that can match a single discussion
to the matching element in your $scope.friends
. That function might look like this (this can be improved, but it gets the job done):
我的建议是在您的角度控制器中定义一个函数,该函数可以将单个讨论与$scope.friends中的匹配元素匹配。这个函数可能看起来像这样(可以改进,但它可以完成工作):
$scope.findMatchingId = function (discussion) {
var matchingFriend;
$scope.friends.some(function (friend) {
if (friend.id === discussion.recipient.id) {
matchingFriend = friend;
return true;
}
return false;
});
return matchingFriend;
};
Then in your html, you can just call that function on the element within the ng-repeat like this:
然后在html中,只需调用ng-repeat元素中的函数即可:
<div ng-repeat="discussion in discussions">
<div>Online = {{findMatchingId(discussion)}}</div>
</div>
See this plnkr
看到这个plnkr