nowaySync nowaySync - 11 days ago 6
AngularJS Question

How take value from matematic operation as string and modify in angular js?

I have an array of string into html page with math operation like "2","*","3","-","1", in html body i have used:

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


in js

$scope.math = [];
$scope.expression = $scope.math.join('');


I need to take this value and modify into js, as example
expression + 1 then show 6. i need to take the result 5 and sum to 1
Thanks for answer.

Answer

You can use eval in controller as well. For example:

Controller:

 $scope.math = ["2","*","3","-","1"];
 $scope.expression = $scope.math.join('');
 $scope.modifiedExpression = eval($scope.expression) + 1;

HTML:

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

Result:

5
6

See http://jsfiddle.net/xfrjdtrb/

Comments