Che*_*Lin 73 javascript angularjs
数字字符串'5'
var num_str = '5';
Run Code Online (Sandbox Code Playgroud)
如何解析Int并让下面的答案同时正确?
{{num_str + 1}} // 6
{{num_str - 1}} // 4
Run Code Online (Sandbox Code Playgroud)
parseInt不能在Angular表达式中使用,
{{parseInt(num_str) - 1}}
Run Code Online (Sandbox Code Playgroud)
数字过滤器不能做加减,
{{num_str - 1 | number}}
Run Code Online (Sandbox Code Playgroud)
如果有人有有用的建议,我将非常感谢你
rg8*_*g88 86
在你的控制器中:
$scope.num_str = parseInt(num_str, 10); // parseInt with radix
Run Code Online (Sandbox Code Playgroud)
has*_*sin 77
我更喜欢使用角度滤镜.
app.filter('num', function() {
return function(input) {
return parseInt(input, 10);
};
});
Run Code Online (Sandbox Code Playgroud)
然后你可以在dom中使用它:
{{'10'|num}}
Run Code Online (Sandbox Code Playgroud)
这是一个小提琴.
希望这有帮助!
CD.*_*D.. 54
你可以试试:
{{ 1 * num_str + 1 }}
Run Code Online (Sandbox Code Playgroud)
Mos*_*sho 22
另一种选择是:
$scope.parseInt = parseInt;
Run Code Online (Sandbox Code Playgroud)
然后你可以像你想要的那样做:
{{parseInt(num_str)-1}}
Run Code Online (Sandbox Code Playgroud)
这是因为角度表达式无法访问window,只能访问scope.
此外,使用数字过滤器,将表达式包装在括号中:
{{(num_str-1) | number}}
Run Code Online (Sandbox Code Playgroud)
D. *_*ott 10
以上都不适合我.
但这样做:
{{ (num1_str * 1) + (num2_str * 1) }}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
225184 次 |
| 最近记录: |