116

Angularでは、スコープ内に多くのオブジェクトを返すオブジェクトがあります。それぞれにIDがあります(これはフラットファイルに保存されているため、DBはなく、ユーザーにはできないようですng-resource

私のコントローラーでは:

$scope.fish = [
    {category:'freshwater', id:'1', name: 'trout', more:'false'},
    {category:'freshwater', id:'2', name:'bass', more:'false'}
];

私の見解では、デフォルトで隠されている魚に関する追加情報がありng-showますが、単純な[もっと見る]タブをクリックすると、関数を呼び出したいと思いますshowdetails(fish.fish_id)。私の関数は次のようになります。

$scope.showdetails = function(fish_id) {  
    var fish = $scope.fish.get({id: fish_id});
    fish.more = true;
}

これで、ビューに詳細が表示されます。fishただし、ドキュメントを検索した後、その配列を検索する方法がわかりません。

では、配列をクエリするにはどうすればよいですか?$scopeまた、コンソールでデバッガーを呼び出して、オブジェクトを操作するにはどうすればよいですか?

4

7 に答える 7

211

既存の$filterサービスを使用できます。http://jsfiddle.net/gbW8Z/12/の上のフィドルを更新しました

 $scope.showdetails = function(fish_id) {
     var found = $filter('filter')($scope.fish, {id: fish_id}, true);
     if (found.length) {
         $scope.selected = JSON.stringify(found[0]);
     } else {
         $scope.selected = 'Not found';
     }
 }

Angularのドキュメントはこちらhttp://docs.angularjs.org/api/ng.filter:filter

于 2013-09-27T06:29:30.337 に答える
95

それがあなたを少し助けることができるかどうか私は知っています。

これが私があなたのためにシミュレートしようとしたものです。

jsFiddleをチェックアウトしてください;)

http://jsfiddle.net/migontech/gbW8Z/5/

'ng-repeat'でも使用できるフィルターを作成しました

app.filter('getById', function() {
  return function(input, id) {
    var i=0, len=input.length;
    for (; i<len; i++) {
      if (+input[i].id == +id) {
        return input[i];
      }
    }
    return null;
  }
});

コントローラでの使用法:

app.controller('SomeController', ['$scope', '$filter', function($scope, $filter) {
     $scope.fish = [{category:'freshwater', id:'1', name: 'trout', more:'false'},  {category:'freshwater', id:'2', name:'bass', more:'false'}]

     $scope.showdetails = function(fish_id){
         var found = $filter('getById')($scope.fish, fish_id);
         console.log(found);
         $scope.selected = JSON.stringify(found);
     }
}]);

ご不明な点がございましたら、お気軽にお問い合わせください。

于 2013-03-25T09:01:54.793 に答える
22

@migontechの回答と彼のアドレスに、「おそらくもっと一般的にすることができる」という彼のコメントを追加するために、これを行う方法を示します。以下では、任意のプロパティで検索できます。

.filter('getByProperty', function() {
    return function(propertyName, propertyValue, collection) {
        var i=0, len=collection.length;
        for (; i<len; i++) {
            if (collection[i][propertyName] == +propertyValue) {
                return collection[i];
            }
        }
        return null;
    }
});

フィルタの呼び出しは次のようになります。

var found = $filter('getByProperty')('id', fish_id, $scope.fish);

文字列ベースの一致を可能にするために、単項(+)演算子を削除したことに注意してください...

于 2013-10-03T21:33:21.913 に答える
13

汚くて簡単な解決策は次のようになります

$scope.showdetails = function(fish_id) {
    angular.forEach($scope.fish, function(fish, key) {
        fish.more = fish.id == fish_id;
    });
};
于 2013-03-25T09:09:41.607 に答える
7

Angularjsには、これを行うためのフィルターオプションがすでにあります、 https: //docs.angularjs.org/api/ng/filter/filter

于 2015-03-08T09:16:27.287 に答える
7

あなたの解決策は正しいですが、不必要に複雑です。純粋なJavaScriptフィルター関数を使用できます。これはあなたのモデルです:

     $scope.fishes = [{category:'freshwater', id:'1', name: 'trout', more:'false'},  {category:'freshwater', id:'2', name:'bass', more:'false'}];

そしてこれがあなたの機能です:

     $scope.showdetails = function(fish_id){
         var found = $scope.fishes.filter({id : fish_id});
         return found;
     };

次の式を使用することもできます。

     $scope.showdetails = function(fish_id){
         var found = $scope.fishes.filter(function(fish){ return fish.id === fish_id });
         return found;
     };

この関数の詳細:LINK

于 2016-07-20T14:45:47.647 に答える
4

このスレッドを見ましたが、検索に一致しないIDを検索したかったのです。それを行うためのコード:

found = $filter('filter')($scope.fish, {id: '!fish_id'}, false);
于 2016-09-19T11:14:59.393 に答える