Reputation: 389
Is it possible to get the angular filter return value in controller scope. I need to pass the returned value of one filter to another filter. I have a filter defined as follows
.filter('Filter', Filter)
function Filter($filter) {
return FilterFn;
function FilterFn(data, weight, waist) {
var factor1 = weight * 1.082 + 94.42;
var factor2 = waist * 4.15;
var leanBodyMass = factor1 - factor2;
var bodyFatWeight = weight - leanBodyMass;
var bodyFatResults = bodyFatWeight * 100 / weight;
// return Params
if (bodyFatResults === "-Infinity") {
// $filter('Filter2')(0);
return 0;
} else if (parseFloat(bodyFatResults) < 0) {
// $filter('Filter2')(2);
return 2;
} else {
// $filter('Filter2')(bodyFatResults.toFixed(1));
return bodyFatResults.toFixed(1);
}
}
}
I need to get the returned value of this filter in controller scope.
Upvotes: 0
Views: 1242
Reputation: 431
You need to pass your filter to your controller, for example:
var myApp = angular.module('myApp',[]);
myApp.controller('GreetingController', ['$scope', function($scope) {
$scope.foo = $filter('yourFilter')();
}])
.filter('yourFilter', function() {
return 'hello';
});
Upvotes: 0
Reputation: 311
you can inject the $filter service on your controller and use your custom filters with it:
.controller(["filter", function($filter) {
var somevariable = $filter("Filter2")(data, weight, waist);
});
See docs
Upvotes: 0
Reputation: 1463
Inject $filter inside your controller and use it like that:
.controller('Controller', function($filter) {
var filtered = $filter('Filter2')(data, weight, waist);
});
Upvotes: 2