Utilizing an element directive across multiple views, the directive iterates through each 'resource' in a list of resources using
ng-repeat="resource in resources"
.
Different page controllers determine which resources are fetched from the API by having a unique $scope.resourceApiPath
in each view controller. The directive controller then uses this path to make an $http
call.
This setup works effectively for displaying different types of resources on different views - such as the user's own resources, public resources, and starred resources.
The challenge arises when I want to include a filter on the ng-repeat
in one specific view:
ng-repeat="resource in resources | filter:resourceSearch.text"
. Is it possible to conditionally add this filter based on a variable like $scope.filter == true;
, otherwise default to ng-repeat="resource in resources"
?
Here is my directive:
.directive('resourcePanel', function() {
return {
restrict: 'E',
scope: false,
templateUrl: 'scripts/templates/resource-panel.html',
controller: 'ResourcesPanelController'
}
})
The ResourcesPanelController
referenced in the directive includes code similar to this:
$scope.resources = [];
$scope.loadResources = function() {
$scope.resources = []; // Clear array
$http.get('/api/resource' + $scope.resourceApiPath)
.then(function(res) {
$scope.resources = res.data.message;
});
};
$scope.loadResources();
The template
scripts/templates/resource-panel.html
looks something like this:
<div class="panel" ng-repeat="resource in resources">
{{resource.content}}
</div>
Thank you!