i've created an application in angular for performing a simple calculator.
The application works fine (demo) but here in the controller within the calculate method i've wrote some arithmetic calculation in javascript. So to make my angular code clean i created another file named common.js where i placed that arithmetic calculations. I'm using lodash.js. but when i tried to call the method _.calculateResult($scope.firstNumber, $scope.secondNumber, $scope.selectedOperator );
i'm getting the following exception.
TypeError: Object function lodash(value) {
// don't wrap if already wrapped, even if wrapped by a different `lodash` constructor
return (value && typeof value == 'object' && !isArray(value) && hasOwnProperty.call(value, '__wrapped__'))
? ...<omitted>... } has no method 'calculateResult'
at Object.$scope.calculate (http://localhost:8080/RestSample/app/scripts/controllers.js:18:19)
can anyone please tell me some solution for this.
common.js
(function() {
var calculateResult = function(no1, no2, opp) {
var A = parseInt(no1);
var B = parseInt(no2);
var C = 0;
switch (opp) {
case '+':
C = A + B;
break;
case '-':
C = A - B;
break;
case '*':
C = A * B;
break;
case '/':
C = A / B;
break;
}
return C;
}
})();
controllers.js
var app = angular.module('app', []);
app.controller("appController", function($scope){
$scope.operators = ['+', '-', '*', '/'];
$scope.selectedOperator = $scope.operators[0];
$scope.calculate = function() {
$scope.result = _.calculateResult($scope.firstNumber, $scope.secondNumber, $scope.selectedOperator );
};
});
index.html
<div ng-app="app">
<div ng-controller="appController">
<div class="offset4 span6 well">
<label>Enter a value :</label>
<input ng-model="firstNumber" type="text"> <br/><br>
<label>Another Value:</label>
<input ng-model="secondNumber" type="text"> <br/><br>
<label>Operator:</label>
<select ng-model="selectedOperator"
ng-options="operator for operator in operators"></select>
<br><br>
<button ng-click="calculate()">Calculate</button> <br><br>
Result: {{result}}
</div>
</div>
</div>