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
There is a good description of the answer to your question at the following address:
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.
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...
- Return a special response if the session has timed out.
- Preserve the name of the $scope function that made the failed AJAX call and any arguments passed to it.
- Display a modal for the user to enter their password.
- From that modal, make a special AJAX call to authenticate the user.
- 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
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.