javascript AngularJS 视图中的字符串到整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23544436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String to integer in AngularJS view
提问by Prashobh
I know how to convert a string to an integer in the controller
我知道如何在控制器中将字符串转换为整数
parseInt( $scope.date)
But i have to do this in the view
但我必须在视图中这样做
<span data-ng-show="item.date">{{ item.date | hbdatetime }}</span>
In the view itself i have to convert the date ( which i am getting as a string ) to an integer so that i can filter with hbdatetime .
在视图本身中,我必须将日期(我作为字符串获取)转换为整数,以便我可以使用 hbdatetime 进行过滤。
I am trying something like this
我正在尝试这样的事情
// <data-ng-show="item.date">parseInt{{ item.date | hbdatetime }}</span>
Please help in angular way
请以角度方式提供帮助
回答by Jens Neubauer
You need to place the function call to parseIntinside the expression:
您需要将函数调用parseInt放在表达式中:
<span data-ng-show="item.date">{{ parseInt(item.date) | hbdatetime }}</span>
Angular will pass the value of item.dateto the function and then apply the filter to the return value.
Angular 会将item.date的值传递给函数,然后将过滤器应用于返回值。
You might want to consider writing your own filter that can handle a string as input.
您可能需要考虑编写自己的过滤器来处理作为输入的字符串。