在我的 ng-app 上调用 ng-strict-di 时,我收到一条错误消息“错误:[$injector:strictdi] percentageFilter 未使用显式注释,无法在严格模式下调用”
<span ng-hide="item.edit" ng-bind="item.tax | percentage"></span>
我尝试在注入(inject)中使用字符串“percentage”和 percentageFilter”来解决问题。
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 模式下注入(inject)百分比过滤器或任何其他过滤器的正确方法是什么?
有问题的过滤器:
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) + '%';
};
});
最佳答案
问题不在于您注入(inject)过滤器的方式,而在于过滤器本身。您没有对 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) + '%';
};
}]);
关于javascript - 在 Controller 中注入(inject)百分比过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27950052/