【问题标题】:How to filter ng-repeat in Angular by certain property value如何通过某些属性值过滤Angular中的ng-repeat
【发布时间】:2016-07-26 01:40:39
【问题描述】:

我正在尝试使用 ng-repeat 显示一个对象数组,但只显示属性值为的对象

“类别”:“问题”

这是我的看法

<div class="small-6 large-6 columns" ng-repeat="post in posts | filter: { category: Problems }">
  <span><a href="#/posts/{{getSlug(post.title)}}"><img src='img/posts/{{post.img}}.jpg' /></a></span>
  <div class="category"><a href="#">{{post.category}}</a></div>
  <a class="title" href="#/posts/{{getSlug(post.title)}}">{{post.title}}</a>
  <div class="meta"><span class="date">{{post.date | date:'dd MMMM yyyy'}}</span></div>
</div>

如果需要还有控制器

.controller('PostListController', ['$scope', '$http', function($scope, $http){

    $http.get('data/posts.json').success(function(response){
      $scope.posts = response;
      $scope.getSlug = function(text){
          return text.replace(/\W+/g, '-');
        };
    });
}])

知道为什么这不起作用吗?

提前致谢。

【问题讨论】:

标签: angularjs angularjs-ng-repeat ng-repeat angularjs-filter angular-filters


【解决方案1】:

语法错误,在Problems 周围添加单引号,如:

<div class="small-6 large-6 columns" ng-repeat="post in posts | filter: { category: 'Problems'}">

Angular 默默地将Problems(作为变量)确定为undefined,而不是抛出ReferenceError

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    • 1970-01-01
    • 2014-05-30
    相关资源
    最近更新 更多