angularjs - Call Angular $filter using expression string -


i trying call $filter using filter expression via filter string i've stored metadata. instance filter string might this:

var filterformyvalue = "number : 2 | someotherfilter"; 

this no problem invoking similar hardcoded filter via markup:

<span>{{ somevalue |  number : 2 | someotherfilter</span> 

however want programmatically apply filter. doing $filter(myfilterstring)(valuetofilter) doesn't work since can't include filter parameters or multiple chained filters single string. allow pass filter name , parameters must passed separately don't want since generic method needs apply filter string value. thought $parse might of use unable find examples of how might combined $filter achieve this.

this in lines of bluetoft comment, maybe 1 step closer using parser service instead of compile.

http://plnkr.co/edit/zyfz42xauue4trzbxkqn?p=preview

$scope.item = 1.123;  //option 1 var filterfn = $parse('item |number:2|myfilter'); console.log(filterfn($scope));  //option 2 $scope.item = $filter('number')($scope.item, 2); $scope.item = $filter('myfilter')($scope.item); console.log($scope.item); 

there 2 options. option1 uses parser , option may can create custom filter chain service (this internally parser service more in terms of expected pattern of passing input/filters).


Comments