Conditionally apply filters with ng-repeat

前端 未结 3 558
忘了有多久
忘了有多久 2021-01-04 18:44

I have an object that contains a mixture of numbers and text for values. I\'d like to apply the numbers filter to the object\'s value when it\'s a number (obvio

3条回答
  •  一向
    一向 (楼主)
    2021-01-04 19:13

    You could try it this way - In your controller, you can have a function which identifies if the provided value is a string or a number:

    $scope.isNumber = function (value) {
        return angular.isNumber(value);
    };
    

    Next, in your view you could have the following:

    {{metric}} {{metricData | number}} {{metricData}}

    Thus, when the metricData is a number, it is filtered and when it is a string, it is output as it is.

提交回复
热议问题