I'm trying to figure out how eval() works in AngularJS, but I can't seem get my head around it.
I have the following:
$scope.salaryRate, $scope.priceRate = {
number: undefined,
type: undefined,
from: undefined,
to: undefined,
rate: undefined
}
$scope.addRate = function (variable) {
eval('$scope.' + variable).push({
'number': eval('$scope.' + variable + 'Rate').number,
'type': eval('$scope.' + variable + 'Rate').type,
'days': {
'days': eval('$scope.' + variable + 'SelectedDay'),
'daynames': displayDayNames($scope.dayNames, eval('$scope.' + variable + 'SelectedDay'))},
'from': eval('$scope.' + variable + 'Rate').from,
'to': eval('$scope.' + variable + 'Rate').to,
'rate': eval('$scope.' + variable + 'Rate').rate,
'id': eval(variable + 'Number')
});
eval('$scope.' + variable + 'Rate') = {
number: undefined,
type: undefined,
from: undefined,
to: undefined,
rate: undefined
};
}
For some reason eval works fine when I am pushing values to a variable array. However for some reason eval('$scope.' + variable + 'Rate') =
doesnt work and I get the error ReferenceError: Invalid left-hand side in assignment
.
I am trying to make my function dynamic, so that I can use it for more than one $scope
. How do I solve this with AngularJS?
It also happens to fail at this point
function findRowIndex(variable, id){
eval('var ' + variable + 'Rows') = eval('$scope.' + variable);
}