我编写了一个社交媒体应用程序。我的个人资料页面中的个人资料菜单有问题。我希望它根据某些情况成为可见的菜单项。我确实将菜单项放入这样的数组中;
$scope.menuitems = [
{id : "1", name : "Message", show : "other", url : ""},
{id : "2", name : "Follow", show : "other", url : ""},
{id : "3", name : "Followers", show : "all", url : ""},
{id : "4", name : "About", show : "all", url : ""},
{id : "5", name : "Statistics", show : "all", url : ""},
{id : "6", name : "Edit", show : "own", url:""}
];
如果可见的配置文件是用户自己的配置文件,我想打印具有“show”值“own”的项目。如果可见的配置文件是另一个用户的配置文件,我想打印具有“显示”值“其他”的项目。我想在每个状态中打印具有“显示”值“全部”的项目。我为此在互联网上做了一些研究,但我想我还没有找到合适的词。我如何在 Angularjs 上使用 ng-repeat 来实现这个?
最佳答案
创建自定义过滤器
$scope.conditionVar = 'own'; //this will change depending on what profile
angular.module('myFilters', []).
filter('profilefilter', function() {
return function(items, condition) {
var out = [];
for (var i in items) {
var item = items[i];
if (item.show === 'all' || item.show === condition) {
out.push(item);
}
}
return out;
}
});
<li ng-repeat="menuitem in menuitems | profilefilter:conditionVar">{{menuitem}}</li>
这是工作演示 - https://jsfiddle.net/0o7ewpgd/1/
关于angularjs - 我怎样才能用 ng-repeat 制作这个过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41900307/