如何使用ng-repeat来制作这个过滤器

时间:2022-11-16 20:34:55

i coding a social media aplication. I have a problem with profile menu in profile page. I want it to be visible menu items depending on certain circumstancesces. I did put menu items in a array like this;

我编写了一个社交媒体应用程序。我对配置文件页面中的配置文件菜单有问题。我希望它是可视的菜单项,这取决于特定的情况。我确实把菜单项放在这样的数组中;

$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:""}
];

İf the visible profile is the user's own profile, i want to print items with 'show' value 'own'. If the visible profile is a another user's profile, i want to print items with 'show' value 'other'. And i want to print items with 'show' value 'all' in every stuation. I did a little research on the internet for this but I guess I have not found the right words. How i make this with ng-repeat on Angularjs?

İf可见概要文件用户的配置文件,我想打印项目“自己”与“秀”价值。如果可见的配置文件是另一个用户的配置文件,我想打印带有“show' value 'other”的项目。我想在每一篇文章中都打印带有“显示”“全部”的内容。我在网上做了一些调查,但是我想我没有找到合适的词。如何在Angularjs上使用ng-repeat ?

1 个解决方案

#1


2  

Create a custom filter

创建一个自定义过滤器

    $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>

here is the working demo - https://jsfiddle.net/0o7ewpgd/1/

这里是工作演示——https://jsfiddle.net/0o7ewpgd/1/

#1


2  

Create a custom filter

创建一个自定义过滤器

    $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>

here is the working demo - https://jsfiddle.net/0o7ewpgd/1/

这里是工作演示——https://jsfiddle.net/0o7ewpgd/1/