【问题标题】:Angularjs filter not working with ng-repeatAngularjs过滤器不适用于ng-repeat
【发布时间】:2016-10-06 22:00:52
【问题描述】:

当我使用函数更改过滤器值时,Angularjs 过滤器无法与 ng-repeat 一起使用...我是 angularjs 的新手。

这些是我的 html 按钮,必须使用“ng-click”上的功能更改过滤器值;

<span>
    <a class="button button-stable" ng-class="class('static')"
       ng-click="activate('static','','')">
        All
    </a>
</span>
<span ng-repeat="profiles in cardList | unique:'profile'">
    <a class="button button-stable" ng-class="class($index)"
       ng-click="activate($index,profiles,profiles.profile);">
        {{profiles.profile}}
    </a>
</span>

这些是我的控制器功能;

$scope.cardList = [
    {id: 1, serial_number: '35986580', card_number: 'BG9S8W7DJSKLA9', profile: '512', expiry_date: '5-1-2017'},
    {id: 2, serial_number: '35986581', card_number: 'ASLWODS6F5812H', profile: '512', expiry_date: '5-1-2017'},
    {id: 3, serial_number: '35986582', card_number: '9A5S218LSKJBMC', profile: '768', expiry_date: '5-1-2017'},
    {id: 4, serial_number: '35986583', card_number: 'XLCKDIGOSJS092', profile: '1024', expiry_date: '5-1-2017'},
    {id: 5, serial_number: '35986584', card_number: 'XKSODKO0CNJSUW', profile: '1024', expiry_date: '5-1-2017'},
    {id: 6, serial_number: '35986585', card_number: 'PLHG0G9M746172', profile: '2048', expiry_date: '5-1-2017'},
    {id: 7, serial_number: '35986586', card_number: '5DF888F9EGVNDJ', profile: '2048', expiry_date: '5-1-2017'},
    {id: 8, serial_number: '35986587', card_number: 'D6F5G49ILMA9SF', profile: '4096', expiry_date: '5-1-2017'}
];
$scope.class = function(index, item){
    if ($scope.selected == index){
        return 'button-dark active';
    } else {
        $scope.staticAll = '';
        return '';
    }
}
$scope.activate = function(index, item, profile){
    if (index != 'static'){
        $scope.selected = index;
        $scope.filterProfile = profile;
        $scope.staticAll = '';
    } else {
        $scope.selected = 'static';
        $scope.filterProfile = '';
        $scope.staticAll = 'button-dark active';
    }
}

这就是过滤器不起作用的地方;

<tr ng-repeat="card in cardList | filter: filterProfile">
                <td class="center">{{card.profile}}</td>
                <td class="center">{{card.expiry_date}}</td>
                <td class="center"><a class="button button-medium button-energized">Assign</a></td>
            </tr>

这是一张图片;

我想用配置文件按钮过滤结果,但 'filterProfile' 的值没有改变...

请帮忙...

【问题讨论】:

  • 这似乎工作正常here(显然风格不同)。
  • 是的,我不小心在桌子前结束了 ng-controller div。 &lt;div ng-controller="cardscontroller"&gt;..lots..of..html...&lt;/div&gt;&lt;table&gt;&lt;tr ng-repeat='card in cardList | filter: filterProfile'&gt;.....card-details...&lt;/tr&gt;&lt;/table&gt; 无论如何,谢谢先生...

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


【解决方案1】:

你应该做的是将模型带到它的过滤器选项中,我想特别是带有这些数字的 div。

现在我不太了解武器配置文件的愿望清单,但是在他们的配置文件中,您必须提供 ng-model 例如ng-model = "profileSelected"

然后添加到您的列表中

<ion-item ng-repeat="card in cardList | filter: profileSelected">

为了更好的帮助,你可以创建一个codepen?

【讨论】:

  • 谢谢。这就是问题所在; &lt;div ng-controller="cardsController"&gt;....html...&lt;/div&gt;&lt;table&gt;&lt;tr&gt;&lt;/tr&gt;&lt;tr ng-repeat="card in cardList | filter: filterProfile"&gt; &lt;td class="center"&gt;{{card.profile}}&lt;/td&gt; &lt;td class="center"&gt;{{card.expiry_date}}&lt;/td&gt; &lt;td class="center"&gt;&lt;a class="button button-medium button-energized"&gt;Assign&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;我不小心结束了
    标签。
【解决方案2】:

我不太清楚您想要的最终产品是什么,但我可以告诉您,自定义过滤器不是这样定义的。格式类似于

    var app = angular.module('buttonApp',[]);
    app.filter('myFilter', function() {
    return function(x) {
        //filter goodness in here
      }    
    })

你可以在这里找到过滤器的官方文档https://docs.angularjs.org/tutorial/step_11

【讨论】:

  • OP 到底在哪里定义了任何类型的自定义过滤器?
猜你喜欢
  • 1970-01-01
  • 2015-12-20
  • 1970-01-01
  • 1970-01-01
  • 2016-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多