2013-05-09 50 views
0

ng-repeat绑定集合过滤布尔使用标签,我有一个ng重复,显示一个集合过滤布尔使用制表符(收藏项),当我更新了过滤器不会重新应用的集合。我有:当我更新集合过滤器不重新应用

全部商品|收藏夹

  • 项1
  • 项目2
  • 项目3

当第一负载(使用JSON)从所述服务器收集,凸片工作得很好,并过滤收集。当您点击某个项目时,我会更新该项目的收藏夹属性,但过滤器不会更新,如果您在标签中单击,似乎数据不会更新。

代码:

HTML:

<button class="btn" ng-model="section" value="all-items" ng-click="filterCriteria={}; activate('all-items')" ng-class="{disabled: products.length == 0, active: section == 'all-items'}">All items ({{products.length}})</button> 

<button class="btn" ng-model="section" value="favourites" ng-click="activate('favourites'); filterCriteria.Favourite = true;" ng-class="{disabled: (products | filter: {Favourite:true}).length < 0, active: section == 'favourites'}">Favourites</button> 

<!-- List of products --> 
<ul> 
    <li ng-repeat="product in products | filter: filterCriteria"> 
     <button class="btn" type="button" ng-click="favouriteClick(product)">Favourite</button> 
     <p>{{product.description}}</p> 
    </li> 
</ul> 

控制器

app.controller('ProductListCtrl', ['$scope', '$http', function ($scope, $http, $filter)  { 

    $http.get('/Products').success(function (data) { 
     $scope.products = data; 
    }); 

    $scope.filterCriteria = {}; 
    $scope.section = "all-items"; 

    $scope.activate = function (option) { 
     $scope.section = option; 
    }; 

    $scope.favouriteClick = function (product) { 

     product.favourite = !product.favourite; 

     // Sync with back-end 
     $http({ 
      method: 'POST', 
      url: '/SetFavourite', 
      data: 'name=' + product.Sku + '&value=' + product.favourite, 
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' } 
     }); 
    }; 
} ]); 

任何反馈不胜感激。 感谢

编辑

我创建了一个拨弄解决方案http://jsfiddle.net/njrHm/

+0

加载后'$ scope.products'看起来像什么? – Langdon 2013-05-09 19:00:23

回答

0

这是一个外壳的问题。你有:

ng-click="activate('favourites'); filterCriteria.Favourite = true;" 

...然后你有:

product.favourite = !product.favourite; 

我猜想你要改变你的ngClick使用小写favourite