是否可以在控制器范围内获得角度过滤器返回值。我需要将一个过滤器的返回值传递给另一个过滤器。我有一个过滤器定义如下
.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);
}
}
}
我需要在控制器范围内获取此过滤器的返回值。
答案 0 :(得分:2)
在控制器中注入$ filter并使用它:
.controller('Controller', function($filter) {
var filtered = $filter('Filter2')(data, weight, waist);
});
答案 1 :(得分:0)
您可以在控制器上注入$ filter服务,并使用自定义过滤器:
.controller(["filter", function($filter) {
var somevariable = $filter("Filter2")(data, weight, waist);
});
请参阅docs
答案 2 :(得分:0)
您需要将过滤器传递给控制器,例如:
var myApp = angular.module('myApp',[]);
myApp.controller('GreetingController', ['$scope', function($scope) {
$scope.foo = $filter('yourFilter')();
}])
.filter('yourFilter', function() {
return 'hello';
});