Angular
Calling a $scope function where function name is in a variable
Gravatar is a globally recognized avatar based on your email address. Calling a $scope function where function name is in a variable
  Carl Chambers
  All
  Feb 6, 2018 @ 08:48pm

Hey Rick,

I'm trying to call a function like $scope.myFunc(myArg) where the actual names of myFunc and myArg are stored in variables.
I found a way to do it using eval() but everthing I've read about eval() says to avoid it.
I thought this would be simple but I'm coming up empty.
Do you know how to do this?

Thanks, Carl

Gravatar is a globally recognized avatar based on your email address. re: Calling a $scope function where function name is in a variable
  Harvey Mushman
  Carl Chambers
  Feb 8, 2018 @ 07:49am

There is a good description of the answer to your question at the following address:

Stackoverflow

Maybe this will help...

BTW what are you actually trying to do, maybe there is a simple solution?

Edit: in the above posting notice that if you use eval() in your code, it will violate Chrome security policy and not run. The answer describes this a little but it is a good reason to stay away from eval() in your code.

Gravatar is a globally recognized avatar based on your email address. re: Calling a $scope function where function name is in a variable
  Carl Chambers
  Harvey Mushman
  Feb 8, 2018 @ 09:00am

Thanks Harvey,

I had read that that Stackoverflow article before. I could not get $eval to work.

BTW what are you actually trying to do, maybe there is a simple solution?

This is actually related to this message about trying to trap a session timeout on an AJAX call, authenticate the user and then retry the AJAX call.

To do this I...

  1. Return a special response if the session has timed out.
  2. Preserve the name of the $scope function that made the failed AJAX call and any arguments passed to it.
  3. Display a modal for the user to enter their password.
  4. From that modal, make a special AJAX call to authenticate the user.
  5. If authenticated, retry the $scope function whose name was preserved in step 2.

Retrying the $scope function was my tripping point.
Ultimately what I did was very simple and, while it is not clean, it works.

if ($scope.auth.func) {
    var param = $scope.auth.param;
    switch ($scope.auth.func) {
        case 'function1':
            $scope.function1(param);
            break;
        case 'function2':
            $scope.function2(param);
            break;
        case 'function3':
            $scope.function3(param);
            break;
        };
};

Many may frown on this approach, but it does what I need.

Thanks again.
Carl

Gravatar is a globally recognized avatar based on your email address. re: Calling a $scope function where function name is in a variable
  Harvey Mushman
  Carl Chambers
  Feb 9, 2018 @ 05:22am

I'm not completely understanding what you are trying to do but one tip an angular developer friend gave me a while back, helped me clear up what scope $scope was pointing to... within the callback the scope changed causing me problems at the time.

His suggestion was at the top of my controller to init a var _this = this. Then whenever I wanted to refer back to the original scope within the controller rather than pointing at $scope refer to _this. It cleared up a lot of things for me.

Hope this helps.

© 1996-2024