如何在指令中以编程方式设置过滤器

时间:2017-07-05 03:44:12

标签: angularjs

我有一个角度指令,允许根据bootstrap中的响应断点重新格式化数字;因此,小型设备的缩放数量(1 Mil),桌面设备将获得全部输出(1,000,000)。

在内部,这在数字的两个html版本之间切换,我想要做的是参数化模板HTML中的数字过滤器,以便在某些情况下我可以将其切换为货币。

如何使用通过指令范围设置的过滤器参数作为动态过滤器参数?

angular.module('myapp').directive("responsiveNumber", ['$log', '$filter', ($log, $filter) => {
    return {
        restrict: "E",
        replace: true,
        scope: {
            'value': '=',
            'precision': '=',
            'prefix': '=',
            'filter': '='
        },
        template: '<span>{{::prefix}}<span class="responsive-num-full">    {{::value | number:precision}}</span><span class="responsive-num-short" ng-bind-html="::value | shortNumber:precision"></span></span>',
        link: (scope: any, element: any) => {

        }
    };
}]);

正在使用中:

<responsive-number value="availableFunds" precision="0" prefix="'$'" filter="'currency'"></responsive-number>

1 个答案:

答案 0 :(得分:0)

//try like this..not sure your stuff achieved or not..but this way u can try
angular.module('myapp').directive("responsiveNumber", ['$log', '$filter', ($log, $filter) => {
return {
    restrict: "E",
    replace: true,
    scope: {
        'value': '=',
        'precision': '=',
        'prefix': '=',
        'filter': '='
    },
    template: '<span>{{::prefix}}<span class="responsive-num-full">    {{::value | number:precision}}</span><span class="responsive-num-short" ng-bind-html="::value | shortNumber:precision"></span></span>',
    link: (scope: any, element: any) => {
      $filter('your filter name')(arg1,arg2); //e.g filte,date,currency
    }
};

}]);