【问题标题】:Inject percentage filter in controller在控制器中注入百分比过滤器
【发布时间】: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


【解决方案1】:

问题不在于您注入过滤器的方式,而在于过滤器本身。您没有为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) + '%';
 };
}]);

【讨论】:

    猜你喜欢
    • 2015-01-22
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    • 1970-01-01
    相关资源
    最近更新 更多