如何在ng-model中添加两个值?减法工作正常

时间:2015-12-23 08:33:12

标签: angularjs

我目前正在练习一些基本的angularjs。什么是正确或正确的方式?我对此感到困惑。有人能说得对吗?

你可以在这里测试一下...... http://plnkr.co/edit/xJNWVY6B298eZ46FauNh?p=preview

代码:

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.3/angular.js" data-semver="1.4.3"></script>
    <script src="app.js"></script>
  </head>

  <body>
    <input type="text" ng-model="vm.one"><br><br>
    <input type="text" ng-model="vm.two"><br><br>
    <input type="text" value="{{ vm.one + vm.two }}">

  </body>

</html>

2 个答案:

答案 0 :(得分:2)

问题是您要添加字符串值。我会先尝试将它们转换为数字:

<input type="text" value="{{ +vm.one + +vm.two }}">

Example

答案 1 :(得分:1)

试试这个http://plnkr.co/edit/UuayjLUscGIdLUN3YIxL?p=preview

<input type="number" ng-model="vm.one"><br><br>
<input type="number" ng-model="vm.two"><br><br>
<input type="text" value="{{ vm.one+ vm.two }}">