我正在学习AngularJS。为了实际操作它,我试图创建一个基本的计算器,从输入框读取值,进行基本计算并给出输出。
Here它是:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div data-ng-app=''>
<p>Enter first number: <input type="number" ng-model="input1"></p>
<p>The number you entered is <mark ng-bind="input1"></mark></p>
<p>Enter second number: <input type="number" ng-model="input2"></p>
<p>The number you entered is <mark ng-bind="input2"></mark></p>
<p>
Subtraction: {{ (Property.input1|number) + (Property.input2|number) }}
</p>
<p>
Sum: {{ (Property.input1|number) + (Property.input2|number) }}
</p>
<p>
Product: {{ (Property.input1|number) * (Property.input2|number) }}
</p>
<p>
Division: {{ (Property.input1|number) / (Property.input2|number) }}
</p>
</div>
似乎没有自动计算这些值。有人可以指导吗?
答案 0 :(得分:1)
您误用了角度ng模型变量评估。
例如,对于Sum,您只需要编写
{{input1 + input2}}
这是新的工作代码:
<div data-ng-app=''>
<p>Enter first number: <input type="number" ng-model="input1"></p>
<p>The number you entered is <mark ng-bind="input1"></mark></p>
<p>Enter second number: <input type="number" ng-model="input2"></p>
<p>The number you entered is <mark ng-bind="input2"></mark></p>
<p>
Subtraction: {{ input1 - input2 }}
</p>
<p>
Sum: {{ input1 + input2 }}
</p>
<p>
Product: {{ input1 * input2 }}
</p>
<p>
Division: {{ input1 / input2 }}
</p>
</div>