javascript - 在 Controller 中注入(inject)百分比过滤器

标签 javascript angularjs dependency-injection angularjs-controller angularjs-filter

在我的 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/

相关文章:

javascript - 如何通过javascript中的expiry_time检查 token 的过期时间

angularjs 指令不起作用(node.js 和 angularjs newbee)

javascript - 如何使用 AngularJS 实现像 Google 一样的右上角个人资料按钮?

android - Dagger 1.x 中的单例

c++ - C++ Google Mock 指南提到的依赖注入(inject)

javascript - 使用 jQuery 提交表单的问题

javascript - 是否可以使用谷歌图表创建两侧条形图?

javascript - 如果输入是焦点且在跨度内,则添加类

angularjs - 使用 ngSanitize 允许一些 html 标签

c# - 在项目之间共享 Microsoft.Extensions.DependencyInjection.ServiceProvider 配置