AngularJS如何从一个文本框的前pression里面加号? [英] AngularJS how to add numbers inside the expression from a textbox?

查看:100
本文介绍了AngularJS如何从一个文本框的前pression里面加号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框加入了一些问题,将其放置在另一个文本框。我的情况是这样的:

I have a problem adding a number from a textbox and place it on another textbox. My situation is like this:

我有一个文本框:

<input type="text" ng-model="num1" />

此外,无论输入的是在这个文本框,这将是另一个这样的文本框的值:

And whatever the input is on this textbox, this will be the value of another textbox like this:

<input type="text" value="{{ num1 + 1 }}" />

此安装成功,但有错误显示。当我第一次键入 2013 ,第二个显示 20131 而不是 2014年
我使用过滤器这样也试过:

This setup shows successfully but with errors. When I type 2013 on the first, the second shows 20131 instead of 2014. I also tried using filters like this:

<input type="text" value="{{ num1 + 1 | number}}" />

但不幸的是,这是行不通的。我错过了什么?

but unfortunately, it doesn't work. What did I miss?

推荐答案

一个文本框的值是一个字符串。
您需要将其转换为整数/浮动你可以做,使用 parseInt函数 parseFloat 如果你有小数。默认情况下,你没有可用的parseInt函数方法在你的前pression,但你可以在你的控制器将其添加到适用范围,使其可用:

The value of a textbox is a string. You need to convert it to an integer/float you can do that using parseInt or parseFloat if you have decimals. By default you don't have the parseInt method available in your expression, but you can add it to the scope in your controller to make it available:

$scope.parseInt = parseInt;


<input type="text" value="{{ parseInt(num1) + 1 }}" />

您可以在行动中看到这里

这篇关于AngularJS如何从一个文本框的前pression里面加号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆