I'm creating a calculator app in AngularJS, but am having trouble with ng-repeat and ng-click.
<div class="col-md-3">
<button ng-repeat="op in operations" name="btn{{ op.name }}"
value="{{ op.symbol }}" class="operationbutton"
ng-click="{{ op.function }}"> {{ op.symbol }}
</button>
</div>
In my app.js, inside my controller, I have an operations variable tied to $scope.
$scope.operations = [
{ "symbol": "+", "name": "add", "function": "add()" },
{ "symbol": "-", "name": "subtract", "function": "subtract()" },
{ "symbol": "*", "name": "multiply", "function": "multiply()" },
{ "symbol": "/", "name": "divide", "function": "divide()" }
];
I've tried taking the quotation marks off the function names, but it turns out that JavaScript object values must contain strings.
I could do something like this
ng-click="{{ op.name === 'add' ? add() : op.name === 'subtract' ? subtract()
: op.name === 'multiply' ? multiply() : op.name === 'divide' ? divide() : multiply()}}"
But that seems like a terrible practice. Is there any way I can improve this, so I can call a function based on the operation?
op.functionrather some string expression?