Sharing the code I have put for your answer. (see the plunkr for complete demo)
HTML:
<ul ng-repeat="post in posts">
{{post.title}}
{{post.body}}
<li ng-repeat="(key,val) in allTags|tagsFilter:post.tagIds">{{val}}</li>
</ul>
<h4>Improved tags structure</h4>
<ul ng-repeat="post in posts" class="improved-post">
<h4>{{post.title}}</h4>
<p>{{post.body}}</p>
<li class="tag" ng-repeat="tag in allTagsImproved|tagsFilterImproved:post.tagIds">{{tag.tag}}</li>
</ul>
Sample tags Service:
app.factory("tagsService",function(){
return {
allTagsImproved:[{id:5,tag:'tagfive'}, {id: 1,tag:'one'}, {id:3,tag:'myTag'}, {id: 4,tag: 'cool tag'}, {id:7,tag:'EPIC Tag'}],
allTags : [{5:'tagfive'}, {1:'one'}, {3:'myTag'}, {4:'cool tag'}, {7:'EPIC Tag'}]
}
});
Sample controller:
app.controller('MainCtrl', function($scope, tagsService) {
$scope.name = 'World';
$scope.data = [{
name:""
},{
name:"ahsan"
},{
name:""
},{
name:"mohsin"
}];
$scope.allTags = tagsService.allTags;
$scope.allTagsImproved = tagsService.allTagsImproved;
$scope.posts = [{
title:'my post',
tagIds:[3,5],
body:' post body'
},{
title:'John\'s post',
tagIds:[1,4,7],
body:'John\'s post body'
}];
});
Filter for the data sample you provided:
app.filter("tagsFilter",function(){
return function(allTags,tagIds){
console.log(tagIds)
var filteredValues = [];
for(var index in tagIds){
for(var ind in allTags){
if(allTags[ind].hasOwnProperty(tagIds[index].toString())){
filteredValues.push(allTags[ind]);
}
}
}
return filteredValues;
}
});
The json you've shared as sample had a limitation over ng-repeat over showing the value using dynamic keys (someone can find a way around I'm sure) but I would actually improve the json to a better structure as used in the plunkr. Hope it helps.