코드를 반복 할 때 나는 그것을 싫어합니다. 그래서 이것을하는 더 좋은 방법이 있는지 알고 싶습니다. 2 개의 지시문이 있습니다. 매우 간단하고 매우 유사합니다. 2 개 개의 지침은 다음과 같이 :AngularJS merge 지시문
는.directive('pkFilterProducts', function() {
return {
restrict: 'A',
templateUrl: 'assets/templates/directives/pkFilterProducts.html',
scope: {
products: '=pkFilterProducts',
filters: '='
}
}
})
.directive('pkStateProducts', function() {
return {
restrict: 'A',
templateUrl: 'assets/templates/directives/pkStateProducts.html',
scope: {
products: '=pkStateProducts',
states: '='
}
}
})
유일한 차이점은 하나의 필터를 수용 할 수있다, 다른 하나는 상태를 받아들입니다. 템플릿도 매우 유사합니다 :
<div class="col-md-6">
<h3>Excluded ({{ excluded.length }})</h3>
<div class="table-responsive" ng-show="excluded.length">
<table class="table table-hover">
<thead>
<tr>
<th class="image-column"></th>
<th>Title</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="product in products | exclude: filters as excluded">
<td><img class="img-responsive" ng-src="{{ product.image }}" /></td>
<td>{{ product.shortTitle }}</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="col-md-6">
<h3>Included ({{ included.length }})</h3>
<div class="table-responsive" ng-show="included.length">
<table class="table table-hover">
<thead>
<tr>
<th class="image-column"></th>
<th>Title</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="product in products | include: filters as included">
<td><img class="img-responsive" ng-src="{{ product.image }}" /></td>
<td>{{ product.shortTitle }}</td>
</tr>
</tbody>
</table>
</div>
</div>
및
<div class="col-md-6">
<h3>Excluded ({{ excluded.length }})</h3>
<div class="table-responsive" ng-show="excluded.length">
<table class="table table-hover">
<thead>
<tr>
<th class="image-column"></th>
<th>Title</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="product in products | statesExclude: states as excluded">
<td><img class="img-responsive" ng-src="{{ product.image }}" /></td>
<td>{{ product.shortTitle }}</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="col-md-6">
<h3>Included ({{ included.length }})</h3>
<div class="table-responsive" ng-show="included.length">
<table class="table table-hover">
<thead>
<tr>
<th class="image-column"></th>
<th>Title</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="product in products | statesInclude: states as included">
<td><img class="img-responsive" ng-src="{{ product.image }}" /></td>
<td>{{ product.shortTitle }}</td>
</tr>
</tbody>
</table>
</div>
</div>
다시, 유일하게 다른이
NG 반복 필터입니다.
을 사용하는 대신 또는
을 제외하면을 사용합니다.
상태를 사용합니다. 및
상태를 포함합니다.을 포함합니다. 이것들을 결합 할 수있는 방법이 있습니까? 필터에 변수를 사용할 수 있습니까?
필터에 변수를 사용할 수 없습니다. 필터를 리팩터링하고 'mode'매개 변수를 받아들이고'include'와'stateInclude'를 모으는 커스텀 필터를 만들지 않는다면, 그럴 수 없습니다. 그리고 tbh, 코드가 리팩토링을 위해 울지 않는다면, 지저분한 공통 분모로 그것을 망칠 이유가 없습니다. – estus
http://stackoverflow.com/questions/26710513/angular-filter-name-as-variable filternames를 변수로 사용하여 보여줍니다. – r3plica
이것은 본질적으로 맞춤 필터와 동일한 것으로 좀 더 어색합니다. – estus