2015-10-27 121 views
0

當我有喜劇搜索,然後它也給我們想要的結果
過濾只有標題不與類別。所以計數和結果將是錯誤的。 任何機構都可以告訴我們如何開發自定義過濾器,或者使用其他任何方式。過濾器無法正常工作與組通過角js

我的代碼是

<script> 
var myApp = angular.module('myApp', ['angular.filter']); 

myApp.controller("MyController", function ($scope) 
{ 
    $scope.movies = 
    [ 
     { title: 'The Matrix', rating: 7.5, category: 'Action' }, 
     { title: 'Focus', rating: 6.9, category: 'Comedy' }, 
     { title: 'The Lazarus Effect', rating: 6.4, category: 'Thriller' }, 
     { title: 'Everly', rating: 5.0, category: 'Action' }, 
     { title: 'Maps to the Stars', rating: 7.5, category: 'Drama' } 
    ]; 
});  

</script> 
<div ng-controller="MyController"> 
    <div class="searchBar"> 
     <input type="text" name="title" ng-model="title" placeholder="Search..." /> 
    </div> 
    <ul class="angularList" ng-repeat="(key, value) in movies | groupBy: 'category'"> 
     <h3>{{ key }} ({{ value.length }})</h3> 
     <li ng-repeat="movie in value | filter:title"> 
      <a href="#">{{ movie.title }}</a><br /> 
      <div class="rating_box"> 
       <div class="rating" style="width:{{movie.rating*10}}%"></div> 
      </div> 
     </li>    
    </ul> 
</div> 
+1

使用這樣的事情對標題應用過濾器只NG-在電影中重複=「(鍵,值)|過濾器:{title:title} –

+0

我不明白這個問題,請你舉個例子解釋你在做什麼,你期望發生什麼期望的輸出)以及實際發生了什麼(例如實際輸出)? –

+0

是y ou添加** angular filter.js ** [只需瀏覽一次](https://github.com/a8m/angular-filter) – Sai

回答

0

解決方案上找到:Solution 找你的腳本:

myApp.filter('groupBy', function ($timeout) { 
    return function (data, key) { 
     if (!key) return data; 
     var outputPropertyName = '__groupBy__' + key; 
     if(!data[outputPropertyName]){ 
      var result = {}; 
      for (var i=0;i<data.length;i++) { 
       if (!result[data[i][key]]) 
        result[data[i][key]]=[]; 
       result[data[i][key]].push(data[i]); 
      } 
      Object.defineProperty(data, outputPropertyName, {enumerable:false, configurable:true, writable: false, value:result}); 
      $timeout(function(){delete data[outputPropertyName];},0,false); 
     } 
     return data[outputPropertyName]; 
    }; 
}) 

在HTML:

<div ng-controller="MyController as myCtrl"> 
    <div class="searchBar"> 
     <input type="text" name="title" ng-model="title" placeholder="Search..." /> 
    </div> 
    <ul class="angularList" ng-repeat="(key, value) in (myCtrl.movies | groupBy: 'myCtrl.category')"> 
     <h3>{{ key }} ({{ value.length }})</h3> 
     <li ng-repeat="movie in value | filter:title"> 
      <a href="#">{{ movie.title }}</a><br /> 
      <div class="rating_box"> 
       <div class="rating" style="width:{{movie.rating*10}}%"></div> 
      </div> 
     </li>    
    </ul> 
</div> 
0

我還測試了你的代碼它的正常工作...... 下面代碼工作代碼

<!DOCTYPE html> 

<html ng-app="myApp"> 
<head> 
    <title>TODO supply a title</title> 
    <meta charset="UTF-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0">   
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.4.7/angular-filter.js"></script> 
</head> 
<body> 
<div ng-controller="MainController"> 

    <div class="searchBar"> 
     <input type="text" name="title" ng-model="title" placeholder="Search..." /> 
    </div> 
    <ul class="angularList" ng-repeat="(key, value) in movies | groupBy: 'category'"> 
     <h3>{{ key}} ({{ value.length}})</h3> 
     <li ng-repeat="movie in value| filter:title"> 
      <a href="#">{{ movie.title}}</a><br /> 
      <div class="rating_box"> 
       <div class="rating" style="width:{{movie.rating * 10}}%"></div> 
      </div> 
     </li>    
    </ul> 

</div> 
<script> 
    angular.module('myApp', ['angular.filter']) 

      .controller('MainController', function ($scope) { 
       $scope.movies = 
         [ 
          {title: 'The Matrix', rating: 7.5, category: 'Action'}, 
          {title: 'Focus', rating: 6.9, category: 'Comedy'}, 
          {title: 'The Lazarus Effect', rating: 6.4, category: 'Thriller'}, 
          {title: 'Everly', rating: 5.0, category: 'Action'}, 
          {title: 'Maps to the Stars', rating: 7.5, category: 'Drama'} 
         ]; 
      }); 


</script> 

+0

這不提供問題的答案。要批評或要求作者澄清,在他們的帖子下留下評論 - 你總是可以評論你自己的帖子,一旦你有足夠的[聲譽](http://stackoverflow.com/help/whats-reputation),你會能夠[評論任何帖子](http://stackoverflow.com/help/privileges/comment)。 – sdgluck

+0

感謝sdgluck,之前我添加了代碼,但在這裏沒有添加這就是原因。 – roshini

相關問題