-3

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);
}
11
  • 2
    Why are you using eval? Not a good idea. Commented Mar 29, 2017 at 20:06
  • 2
    Why on earth are you doing this? Commented Mar 29, 2017 at 20:07
  • @Phix, How else would I make a dynamic function where I can input the scope name into the function and modify the scope with the given variable name? The idea is that I have multiple $scope arrays which uses the same functions, which is add, which are exactly the same, besides the variable names. Commented Mar 29, 2017 at 20:09
  • @Pytth As I said, I am trying to avoid using two exactly similar functions that do exactly the same, just on different sets of $scope variables. Commented Mar 29, 2017 at 20:10
  • 1
    @PhyCoMath I see that you already have an answer, so good for you :) Good luck Commented Mar 29, 2017 at 20:53

1 Answer 1

5

Why wouldn't you just use bracket notation? https://jsfiddle.net/duoxoq8j/2/

$scope.addRate = function (variable) {
    $scope[variable].push({
        ...
    });
    $scope[variable + "Rate"] = {
        number: undefined,
        type: undefined,
        from: undefined,
        to: undefined,
        rate: undefined
    };
}

This is just basic javascript functionality and has nothing to do with Angular or scopes in particular.

Sign up to request clarification or add additional context in comments.

5 Comments

Do I have to change my $scopes aswell for this? I got Uncaught SyntaxError: Unexpected token [ when I changed my scopes. I didnt even know this existed...
Check out the fiddle for an example
Nice! I it worked for the scope examples :) but for some reason it breaks on my edit, but I can probably debug it and find out why :) Didn't really know I could use scopes like this, so thank you :)
Everything in javascript is an object. Read this: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
Thanks, I'll take a look on that :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.