nowaySync nowaySync - 14 days ago 5
AngularJS Question

Calculator in AngularJs

I have try to do a calculator in angular js

i have created button

<li ng-repeat="number in numbers">
<button ng-click=calc(number)>{{number}}</button>
</li>


and operators

<li ng-repeat="operator in operators">
<button ng-click=operat(operator)>{{operator}</button>
</li>


controller

app.controller('myControl', ['$scope', function($scope) {

$scope.numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
$scope.operators = ['+', '-', '*', '/'];
$scope.calc = function(valueNum) {
$scope.valore =valueNum;
};
$scope.operat = function(valueOper){
$scope.operatore = valueOper;
};


now how can i do for the operation on html page ?

for example i press 3 then + and 3
what I must declare on html?

i try with this but not run

<p> result {{value}} {{operator}} {{value}} </p>


thanks.

Answer

Using $eval can evaluate value of expression

Following is demo to resolve your issue

In HTML

<p> result {{$eval(expression)}} </p> 

In Controller

        $scope.value1 = "1";
        $scope.operator = "*";
        $scope.value2 = "7"
        $scope.expression = $scope.value1 + $scope.operator + $scope.value2;
Comments