Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用ng repeat有条件地应用过滤器_Javascript_Html_Angularjs_Angularjs Ng Repeat_Angularjs Filter - Fatal编程技术网

Javascript 使用ng repeat有条件地应用过滤器

Javascript 使用ng repeat有条件地应用过滤器,javascript,html,angularjs,angularjs-ng-repeat,angularjs-filter,Javascript,Html,Angularjs,Angularjs Ng Repeat,Angularjs Filter,我有一个对象,它包含数值和文本的混合体。当对象是数字时,我想将numbers过滤器应用于对象的值(显然)。但是当它不是一个数字的时候,我会接受它只是吐出字符串。按原样,将|number应用于值会格式化数字,但会将字符串值保留为空(毕竟,它们不是数字) 我猜它必须是一个定制的过滤器(我还需要制作)。在执行ng repeat时,是否有一种方法可以仅在HTML中执行此操作 <table> <tr ng-repeat="(metric, metricData) in dat

我有一个对象,它包含数值和文本的混合体。当对象是数字时,我想将
numbers
过滤器应用于对象的值(显然)。但是当它不是一个数字的时候,我会接受它只是吐出字符串。按原样,将
|number
应用于值会格式化数字,但会将字符串值保留为空(毕竟,它们不是数字)

我猜它必须是一个定制的过滤器(我还需要制作)。在执行
ng repeat
时,是否有一种方法可以仅在HTML中执行此操作

<table>
      <tr ng-repeat="(metric, metricData) in data">
        <td>{{metric}}</td>
        <td>{{metricData | number}}</td>
      </tr>
</table>

$scope.data = { name:"this is the name", 
                score:48
                outcome:"as expected",
                attendance:820,
                total:212.34
              };

{{metric}}
{{metricData | number}}
$scope.data={name:“这是名称”,
分数:48
结果:“如预期”,
出席人数:820,
总数:212.34
};

您可以这样尝试-在控制器中,您可以使用一个函数来识别提供的值是字符串还是数字:

$scope.isNumber=函数(值){
返回角度.isNumber(值);
};
接下来,在您看来,您可以拥有以下内容:


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

因此,当
metricData
是一个数字时,它将被过滤,当它是一个字符串时,它将按原样输出。

以下是@callmekatootie使用
ng if
(v1.1.5)请求的备选答案版本:


{{metric}}
{{metricData | number}}
{{metricData}}
这样做的优点是只对数值元素运行过滤器。在这种情况下,这可能没有什么好处,但在其他更复杂的过滤器情况下可能有用。为了回答您关于内置的
angular.isNumber
的另一个问题,@callmekatootie在scope函数
isNumber
中使用了它,它只是在视图中使用内置的包装器


我知道这很旧,但我认为最好的解决方案是将逻辑移到过滤器中

app.filter("metricDataFilter", function($filter) {
    return function(value) {
      if(angular.isNumber(value)) {
          return $filter("number", value);  
      }

      return value;
    }  
}
这样,HTML更加简洁,并且不必重新绘制dom元素

<table>
    <tr ng-repeat="(metric, metricData) in data">
        <td>{{metric}}</td>
        <td>{{metricData | metricDataFilter}}</td>
    </tr>
</table>

{{metric}}
{{metricData | metricDataFilter}

正如您所知,客户筛选是一种不错的方法……我想不是。我只是还没学会怎么做。也许是我学习的时候了。ha.+1如果(v.1.1.5+)的话,变体可能会使用
ng,因此在关闭的情况下数据集根本不会被过滤(这可能会节省大量数据集/复杂过滤器的时间)。我喜欢这种解决方案。angular没有内置的isNumber指令吗<代码>角度。isNumber
?我尝试将其用于其他内容,但无法在HTML中使用。@sh0ber如果,使用
ng的正确语法是什么?我刚把我的angular版本升级到1.1.5。但是我没有找到任何关于该指令使用的文档。刚刚被告知“像ng开关一样使用它”。。。您是否愿意使用nf if提交解决方案?谢谢。我认为在a上显示/隐藏可能会导致问题…如果我认为OP也想知道内置函数
angular.isNumber()
是否可以直接在HTML代码中使用,那么最好使用ng if/ui,我认为这是不可能的。对吗?@callmekatootie我想是的,表达式似乎只支持作用域上的变量。
app.filter("metricDataFilter", function($filter) {
    return function(value) {
      if(angular.isNumber(value)) {
          return $filter("number", value);  
      }

      return value;
    }  
}
<table>
    <tr ng-repeat="(metric, metricData) in data">
        <td>{{metric}}</td>
        <td>{{metricData | metricDataFilter}}</td>
    </tr>
</table>