データ テーブルに単純な強調表示メカニズムを設定しようとしています。
<table>
<thead>
<tr>
<th>Name</th>
<th>Owner</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="file in files" highlightable> <!-- Multiple instances of highlightable -->
<td>{{file.name}}</td>
<td>{{file.owner}}</td>
</tr>
</tbody>
</table>
そして、強調表示を処理するディレクティブがあります。をクリックする<tr>
と、最初に他のすべての のハイ<tr>
ライトが解除され、次にクリックされた がハイライトされます。
directive('highlightable', function() {
return {
require: 'highlightable',
controller: function($scope, $element) {
this.unhighlight = function(file) {
$element[0].style.backgroundColor = 'transparent';
};
},
link: function(scope, element, attrs, ctrl) {
var color = '#DEE4FC';
element.bind('click', function(e) {
ctrl.unhighlight(scope.file);
element[0].style.backgroundColor = color;
});
}
};
});
問題は、ディレクティブのコントローラーのすべてのインスタンスにアクセスしていないように見えることです。別のディレクティブを要求する場合、シナリオでそのディレクティブのすべてのインスタンスを要求し、ng-repeat
繰り返されるディレクティブのコントローラー メソッドを介して各インスタンスを操作するにはどうすればよいですか?