1

I have a situation in which there are main functions named as:

Add_<Element_Type>_Page()

where Element_Type is received as a parameter.

I can see two options to handle the invocation of the correct function:

  1. Write a relatively long switch construct covering all possible values of Element_Type, or

  2. Use some existing mechanism/trick (I'm not aware of) through which I can build a string using the received Element_Type and invoke a function whose name is contained in that string.

Needless to say, all the functions that can be invoked this way have exactly the same signature as far as parameters is concerned.

Obviously, Option 2 would be the best (easy to maintain and very general as no changes are needed whenever a new type of pages is added).

Is this possible to achieve within AngularJS framework?

Thanks in advance.

8
  • Are those function written in controller using $scope or vm/ctrl ? Commented Apr 3, 2017 at 10:14
  • Crazy approach, are those functions autogenerated? Please show some code. What should happend in Add_<Element_Type>_Page() and where is Element_Type needed in those function? Commented Apr 3, 2017 at 10:17
  • @tanmay, not sure I follow you, but I think you are referring to Angular 2 while I'm using AngularJS (i.e. 1). Commented Apr 3, 2017 at 10:22
  • @FDavidov no it's Angular1 only. I'm asking where are your functions placed? Controller? Service? Commented Apr 3, 2017 at 10:23
  • @Iin, not sure why "crazy". The functions are NOT generated automatically (though I wish I could...). They are all hardcoded. The only thing I'm looking for is how to call them without the need of a switch construct. Commented Apr 3, 2017 at 10:24

2 Answers 2

1

You can simply do that using the [...] notation instead of .

Like this,

$scope["Add_" + Element_Type + "_Page"]()

will call $scope.Add_test_Page() assuming Element_Type would be test

or if using controllerAs syntax,

vm["Add_" + Element_Type + "_Page"]()

where Element_Type is a received parameter as you mentioned.

If you want to use it inside HTML, you can have a reference to your Element_Type in $scope/vm (whichever way you are using) and access it.

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

2 Comments

I don't know who downvoted your answer, but the first suggestion (using [...]) works like a charm. Than you!!!! (marked as THE ANSWER of course).
@FDavidov yeah it's fine. glad I could help :)
0

Create an attribute with name func-name and pass your dynamic string value which is your function name to that attribute and within your directive you can do something like this.

scope: {
 funcName: '@'
},
controller: function() {
 $scope[$scope.funcName]();
}

Comments

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.