[英]Inject percentage filter in controller
在我的ng-app上调用ng-strict-di时,出现错误“错误:[$ injector:strictdi] percentFilter未使用显式注释,因此无法在严格模式下调用”
<span ng-hide="item.edit" ng-bind="item.tax | percentage"></span>
我尝试在注入中使用字符串“ percentage”和percentFilter“来解决此问题。
angular.module('myApp').controller("transaction", ['$scope', 'CustomerService', '$state', '$filter', '$modal', 'ngTableParams', 'JobService', 'OrderService', 'toastr', '$timeout','percentageFilter', function ($scope, CustomerService, $state, $filter, $modal, ngTableParams, JobService, OrderService, toastr, $timeout,percentageFilter) {}});
在strictdi模式下注入百分比过滤器或任何其他过滤器的正确方法是什么?
有问题的过滤器:
angular.module('myApp').filter('percentage', function ($window) {
return function (input, decimalForm) {
if ($window.isNaN(input))
return '';
else if (input == 0)
return '-';
else return decimalForm ? (input/100).toFixed(2) : (input*100).toFixed(2) + '%';
};
});
问题与注入过滤器的方式无关,而是与过滤器本身有关。 你是不是使用了显式依赖注释percentageFilter
。 即angular.module('mYapp').filter('percentage', ['$window', function ($window) {
尝试:
angular.module('mYapp').filter('percentage', ['$window', function ($window) {
return function (input, decimalForm) {
if ($window.isNaN(input))
return '';
if (input == 0)
return '-';
return decimalForm ? (input/100).toFixed(2) : (input*100).toFixed(2) + '%';
};
}]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.