【问题标题】:Dynamic AngularJS Filters动态 AngularJS 过滤器
【发布时间】:2014-07-31 19:41:51
【问题描述】:

我有一个动态导航菜单,对应于在创建表格时传递的各种过滤器,根据按下的按钮过滤表格中的内容。

我在 $scope 中有一个名为 $scope.uniqueFilter 的变量。选择的过滤器取决于按下的按钮。在链接的ng-click 上,我定义了一个函数,该函数接受按钮参数的输入,将其传递给该函数,然后将$scope.uniqueFilter 设置为等于按钮的特定过滤方法。

单击时,我已经能够按预期传递所需的过滤器,但它们不仅不适用于内容,而且完全阻止了表格的创建。

我尝试重复的行的内容如下所示:

<tr class="progress" ng-repeat="idea in ideas | filter:uniqueFilter" >

改变 uniqueFilter 的按钮设置如下:

<div class="nav">
    <div ng-controller="NavController">
      <p ng-repeat="link in links">
        <block class="button" href="{{link.URL}}" title="{{link.name}}">
          <a href="{{link.URL}}" ng-click="getFilter(link.show)">
            {{link.name}}
          </a>
        </block>
      </p>
  </div>
</div>

这是 NavController 的内容:

app.controller('NavController', ['$scope', 'DataFactory', function($scope, DataFactory){


$scope.links = [
    {'name':'About', //title that will be displayed on the link
    'URL': '#/about', //URL/view that the link will lead to/show
    'show':'(not actually a filter here)' //view may simply change so some info is hidden or revealed
    },
    {'name':'Active',
    'URL': '#/active',
    'show':'active'  //I want all things with active:true
    },
    {'name':'Complete',
    'URL': '#/active', //this is supposed to go to #/active, not a bug
    'show':'!active' //I want all things with active:false
    }
];

$scope.ideas = DataFactory;


$scope.uniqueFilter=''; //variable to allow access to individual filters based on which button is clicked
$scope.getFilter = function(theFilter){ //called onclick for each button and passes in the link.show that each button has, defining the filter to use
    $scope.uniqueFilter = theFilter;  //sets the variable to match desired filter
    alert("I just set the filter to "+$scope.uniqueFilter);
};
}]);

DataFactory,填充表格的内容:

app.factory('DataFactory', function(){
    return [
        {'title':'Title1',
        'A': '80',
        'B': '6',
        'active': true //this is what should determine if it shows or not
        },
       {'title':'Title2',
        'A': '80',
        'B': '6',
        'active': true
        },
        {'title':'Title3',
        'A': '80',
        'B': '6',
        'active': false
        },
    ];
});

我觉得我遗漏了一些明显的东西,但我想我对 Angular 的了解不够彻底,无法找到它。 感谢您提供的任何帮助!

编辑

所以我提出了一些建议的更新。

这是我的 ng-repeat 标记的更好视图:

<table>
    <tr class="title_bar">
        <td>Title</td>
        <td>A</td>
        <td>B</td>
    </tr>

    <tr class="progress" ng-repeat="idea in ideas | filter:uniqueFilter">
<!-- uniqueFilter here does nothing, and {value:true} stops values from appearing,
 but {active:true}(and false) works. But I need this filter to change based on what
 is clicked, hence why I want uniqueFilter to work -->
        <td>{{idea.title}}</td>
        <td>{{idea.A}}</td>
        <td>{{idea.B}}</td>
    </tr>
</table>

这是我将 NavController 链接数组更改为的内容:

$scope.links = [
    {'name':'About', //title that will be displayed on the link
    'URL': '#/about', //URL/view that the link will lead to/show
    'show':'(not actually a filter here)' //view may simply change so some info is hidden or revealed
    },
    {'name':'Active',
    'URL': '#/active',
    'show': {'active': true}  //unsure if this needs to be 'active' or active without quotes; in the DataFactory, it's in quotes
    },
    {'name':'Complete',
    'URL': '#/active',
    'show':{'active': false}
    }
];

【问题讨论】:

    标签: javascript angularjs filter angularjs-ng-repeat angularjs-filter


    【解决方案1】:

    你很接近。您没有显示您的 ng-repeat,但基本上您只是想确保正确设置 uniqueFilter 的措辞...

    {   'name':'Active',
        'URL': '#/active',
        'show': { active:true } // <-- this is what you'd pass to your "filter:" expression
    },
    {   'name':'Complete',
        'URL': '#/active', 
        'show': { active:false } // <-- this is what you'd pass to your "filter:" expression
    }
    

    所以基本上你的 ng-repeat 看起来(有点像)它传递了过滤器需要使用布尔值

    <li ng-repeat="item in items | filter: uniqueFilter">
    
    // which for example would translate into:
    <li ng-repeat="item in items | filter:{value:true}"> // or
    <li ng-repeat="item in items | filter:{value:false}"> 
    // allowing it to work with boolean values
    

    【讨论】:

    • uniqueFilter 变量似乎不起作用。我已经能够使过滤器与 {active:true} 和 {active:false} 硬编码一起使用,(所以现在我知道我的其余代码与此相关是没有错误的),但 uniqueFilter 是不会影响桌子。我刚刚更新了问题中的代码,以向您展示我的最新编辑。有什么想法吗?
    • 您可以使用/不使用引号,两者都可以。看这个例子:我在这里做的基本上和你一样,一定有一些细微的差别导致它不适合你。在这里,我循环浏览过滤器,然后在点击时设置新过滤器。 jsfiddle.net/ExpertSystem/CyDbB
    • 我正在 Plunker 中对其进行测试(因为 $routeProvider 需要 AJAX 请求并且我无法在本地对其进行测试),并且由于某种原因,您的确切代码在 JSFiddle 而不是 Plunker 上运行。我修改它以完全匹配我所拥有的,并且它有效!但不是在 Plunker。猜猜这是一个 Plunker 问题,而不是我的 Angular 问题。让我的应用程序出错是多么愚蠢的事情......感谢您的帮助!很高兴您能够帮助我使其正常工作。
    • 嘿,完美!乐意效劳。布尔过滤器困扰了我一段时间,直到我意识到如何做它们!幸运的是,您现在再也不会遇到这个问题了 :)
    猜你喜欢
    • 2017-06-08
    • 2014-06-09
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    • 2016-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多