Javascript 奇怪的Angularjs数学
我正在学习Angularjs。我试着做一些简单的数学,只是为了得到角度的感觉。我有两个输入字段,我想在这些字段中输入数字,然后在第三个字段中看到相加的总数。事实上,数学变得很奇怪……有些数字加起来很好,有些时候根本不加,有些时候总数是一个随机错误的数字。有什么想法吗?我觉得我的$watch方法做错了。有没有办法用一块$watch来观察两个变量 HTML-Javascript 奇怪的Angularjs数学,javascript,angularjs,watch,Javascript,Angularjs,Watch,我正在学习Angularjs。我试着做一些简单的数学,只是为了得到角度的感觉。我有两个输入字段,我想在这些字段中输入数字,然后在第三个字段中看到相加的总数。事实上,数学变得很奇怪……有些数字加起来很好,有些时候根本不加,有些时候总数是一个随机错误的数字。有什么想法吗?我觉得我的$watch方法做错了。有没有办法用一块$watch来观察两个变量 HTML- <td><input class="form-control" name="account"placeholder="Acc
<td><input class="form-control" name="account"placeholder="Account"></td>
<td><input class="form-control" name="incomeone" ng-model="funding.startingEstimate"></td>
<td class="warning"></td>
<td>{{funding.startingEstimate}}</td>
<td class="warning">$500</td>
<td class="warning">50%</td>
</tr>
<tr>
<td><input class="form-control" value=""name="account"placeholder="Account"></td>
<td><input class="form-control" name="incometwo" ng-model="funding2.startingEstimate"></td>
<td class="warning"></td>
<td>{{funding2.startingEstimate}}</td>
<td class="warning">$500</td>
<td class="warning">50%</td>
</tr>
<tr>
<td><input class="form-control" placeholder="Account" ng-model="r.account"></td>
<td><input class="form-control" name="incometwo" placeholder="Budgeted"></td>
<td class="warning"></td>
<td></td>
<td class="warning">{{funding.needed}}</td>
<td class="warning">50%</td>
</tr>
这句话
$scope.funding.startingEstimate|0 + $scope.funding2.startingEstimate|0
。。。实际上是这样处理的(因为+
运算符的优先级比|
运算符的优先级高):
。。。也就是说,本质上,二进制ORing是两个估计值。而且,二进制或
的结果有时看起来很奇怪。)
添加参数以确定实际需要的操作顺序,如下所示:
($scope.funding.startingEstimate|0) + ($scope.funding2.startingEstimate|0)
正如@KevinB正确地注意到的,您可以将其正确地放入模板中的表达式中:
<td>{{(funding.startingEstimate|0) + (funding2.startingEstimate|0)}}</td>
{(funding.startingEstimate | 0)+(funding2.startingEstimate | 0)}
当然,正确排序操作在这里也很重要。这个表达式
$scope.funding.startingEstimate|0 + $scope.funding2.startingEstimate|0
。。。实际上是这样处理的(因为+
运算符的优先级比|
运算符的优先级高):
。。。也就是说,本质上,二进制ORing是两个估计值。而且,二进制或
的结果有时看起来很奇怪。)
添加参数以确定实际需要的操作顺序,如下所示:
($scope.funding.startingEstimate|0) + ($scope.funding2.startingEstimate|0)
正如@KevinB正确地注意到的,您可以将其正确地放入模板中的表达式中:
<td>{{(funding.startingEstimate|0) + (funding2.startingEstimate|0)}}</td>
{(funding.startingEstimate | 0)+(funding2.startingEstimate | 0)}
当然,正确排序操作在这里也很重要。这个表达式
$scope.funding.startingEstimate|0 + $scope.funding2.startingEstimate|0
。。。实际上是这样处理的(因为+
运算符的优先级比|
运算符的优先级高):
。。。也就是说,本质上,二进制ORing是两个估计值。而且,二进制或
的结果有时看起来很奇怪。)
添加参数以确定实际需要的操作顺序,如下所示:
($scope.funding.startingEstimate|0) + ($scope.funding2.startingEstimate|0)
正如@KevinB正确地注意到的,您可以将其正确地放入模板中的表达式中:
<td>{{(funding.startingEstimate|0) + (funding2.startingEstimate|0)}}</td>
{(funding.startingEstimate | 0)+(funding2.startingEstimate | 0)}
当然,正确排序操作在这里也很重要。这个表达式
$scope.funding.startingEstimate|0 + $scope.funding2.startingEstimate|0
。。。实际上是这样处理的(因为+
运算符的优先级比|
运算符的优先级高):
。。。也就是说,本质上,二进制ORing是两个估计值。而且,二进制或
的结果有时看起来很奇怪。)
添加参数以确定实际需要的操作顺序,如下所示:
($scope.funding.startingEstimate|0) + ($scope.funding2.startingEstimate|0)
正如@KevinB正确地注意到的,您可以将其正确地放入模板中的表达式中:
<td>{{(funding.startingEstimate|0) + (funding2.startingEstimate|0)}}</td>
{(funding.startingEstimate | 0)+(funding2.startingEstimate | 0)}
当然,正确排序操作在这里也很重要。您似乎认为|
比+
具有更高的优先级。不是的。)如果您愿意,您应该能够在控制器中没有任何内容的情况下完成这项工作<代码>{(funding.startingEstimate | 0)+(funding2.startingEstimate | 0)}
@KevinB介意我把这个添加到答案中吗?)当然不过我很好奇。。。他还能做{{computeNeeded()}}
,这样他还可以做更高级的计算吗?我是一个安格拉斯人。:)(哈,有趣的打字。{{computerNeeded()}}
)如果计算的
已经在范围
上定义了,为什么不呢?不过,对于全局变量,不太确定。您似乎认为|
比+
具有更高的优先级。不是的。)如果您愿意,您应该能够在控制器中没有任何内容的情况下完成这项工作<代码>{(funding.startingEstimate | 0)+(funding2.startingEstimate | 0)}
@KevinB介意我把这个添加到答案中吗?)当然不过我很好奇。。。他还能做{{computeNeeded()}}
,这样他还可以做更高级的计算吗?我是一个安格拉斯人。:)(哈,有趣的打字。{{computerNeeded()}}
)如果计算的
已经在范围
上定义了,为什么不呢?不过,对于全局变量,不太确定。您似乎认为|
比+
具有更高的优先级。不是的。)如果您愿意,您应该能够在控制器中没有任何内容的情况下完成这项工作<代码>{(funding.startingEstimate | 0)+(funding2.startingEstimate | 0)}
@KevinB介意我把这个添加到答案中吗?)当然不过我很好奇。。。他还能做{{computeNeeded()}}
,这样他还可以做更高级的计算吗?我是一个安格拉斯人。:)(哈,有趣的打字。{{computerNeeded()}}
)如果计算的
已经在范围
上定义了,为什么不呢?不过,对于全局变量,不太确定。您似乎认为|
比+
具有更高的优先级。不是的。)如果您愿意,您应该能够在控制器中没有任何内容的情况下完成这项工作<代码>{(funding.startingEstimate | 0)+(funding2.startingEstimate | 0)}
@KevinB介意我把这个添加到答案中吗?)当然不过我很好奇。。。他还能做{{computeNeeded()}}
,这样他还可以做更高级的计算吗?我是一个安格拉斯人。:)(哈,有趣的打字。{{computerNeeded()}}
)如果计算的
已经在范围
上定义了,为什么不呢?但对于全球选手来说,不太确定。太棒了!啊,只是把它们放进去的问题。这让我快发疯了。我以为| 0会将一个数字字符串转换为int。我也尝试在一些地方使用parseInt(),但没有成功。多谢各位!令人惊叹的!啊,只是把它们放进去的问题。这让我快发疯了。我以为| 0会