0

ここでサンプルアプリを作成します。私のアプリには、スコープ内に と という名前のオブジェクトの 2 つの配列がありdataますdata2。また、私は2つのディレクティブを持っています:

app.directive('root',function(){
return {
scope:{
  items:'='
},
restrict:'AE',
template:'<ul><li>Item<ul><li ng-repeat="item in items"><show-item mo="item"></show-item>l</li></ul></li></ul>'
  };
});
 app.directive('showItem',function(){
return {
restrict:'EA',
scope:{
  mo:'='
},
controller:function(){

},
template:'<span>{{mo.name}}</span>'
};
});

モデルで名前で検索するための入力検索が1つあります。しかし、これら2つのモデルを1つの入力で同じクエリで検索するにはどうすればよいですか

4

1 に答える 1

1

モデルを検索入力に添付します。

<input type="search" ng-model="search" />

このモデルの双方向バインディングをディレクティブのスコープに追加し、それを使用して ng-repeat アイテムをフィルタリングします。

<root items="data" search="search"></root> 
<root items="data2" search="search"></root>

app.directive('root',function(){
  return {
    scope:{
      items:'=',
      search:'='
    }, 
    restrict:'AE',
    template:'<ul><li>Item<ul><li ng-repeat="item in items | filter:search"><show-item mo="item"></show-item>l</li></ul></li></ul>'
  };
});

更新された JS ビン

于 2014-05-03T11:07:53.603 に答える