Skip to content Skip to sidebar Skip to footer

How Can I Pass A Filter Into An Angular Directive To Be Used In An Ng-repeat?

I'm trying to set up a directive so that I can pass in both the data to be used in the directive, and the specific filter that will be used in the ng-repeat inside of it. This isn

Solution 1:

Ok, so I think this is a workable solution - rather than passing in the filter, pass in the filtered list like so:

<!DOCTYPE HTML><htmlng-app="MyApp"><head><metacharset="UTF-8"><scripttype="text/javascript"src="angular.min.js"></script><script>var myApp = angular.module('MyApp', []);
myApp.controller('mainPage',function($scope){
    $scope.cars = ["Saab","Volvo","BMW"];
    });
myApp.directive('carList',function(){
    return {
        scope: {
        listOfCars: '&',
        },
        restrict: 'E',
        replace: 'true',
        template: '<table>\
    <tr ng-repeat="car in listOfCars()">\
    <td>{{car}}</td>\
    </tr>\
    </table>'
    };
});
    </script></head><bodyng-controller="mainPage"><car-listlist-of-cars="cars | orderBy:'toString()' | limitTo:2 " ></car-list></body></html>

Which doesn't feel very intuitive. Is this the right approach?

Post a Comment for "How Can I Pass A Filter Into An Angular Directive To Be Used In An Ng-repeat?"