【发布时间】:2015-03-13 01:21:13
【问题描述】:
在我的 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) {}});
在严格模式下注入百分比过滤器或任何其他过滤器的正确方法是什么?
有问题的过滤器:
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) + '%';
};
});
【问题讨论】:
-
我认为没有内置的
percentage过滤器。你创建了一个吗? -
这里有一个答案,您可以首先查看创建过滤器。 stackoverflow.com/questions/13668440/…
-
您的代码还有其他问题。自定义过滤器可以与 ng-strict-di 配合使用:plnkr.co/edit/EDFLZecdjhFawbLQu1Ji?p=preview
-
@NewDev 代码中的问题是没有内置百分比过滤器。
-
那么你的过滤器代码没有使用正确的依赖注释。您需要显示过滤器而不是您使用它的位置。
标签: javascript angularjs dependency-injection angularjs-controller angularjs-filter