My goal is to create a calculator that computes the total of two input fields: price and quantity. I am utilizing AngularJs within the Ionic framework for this project.
Here is my controller.js file:
angular.module('starter.controllers', [])
.controller('DashCtrl', function($scope) {
$scope.a = 0;
$scope.b = 0;
$scope.c = $scope.a * $scope.b;
})
Within my form.html file:
<ion-view view-title="Calculator">
<ion-content class="padding">
<div class="list" ng-controller="DashCtrl">
<label class="item item-input">
<span class="input-label">Price</span>
<input type="text" ng-model="a">
</label>
<label class="item item-input">
<span class="input-label">Qty</span>
<input type="text" ng-model="b">
</label>
<label class="item item-input">
<span class="input-label">Total</span>
<input type="text" value="{{ c | currency:'Rp ' }}">
</label>
</div>
</ion-content>
</ion-view>
Note:
Price = 5000
Qty = 2
By writing {{ a*b }}, you can easily get Rp 10,000.00 printed on the screen. However, I prefer all calculations to be handled by the controller itself.
What script should I implement in the controller to achieve accurate results?