Reputation: 3014
I have an ng-click
that I need to call one of 4 methods depending on a variable. I am displaying certain content on the page depending on something I call {{vm.class}}
, which could be the word Group
for example. Depending on if it's Group
or not I want to call the method createGroup()
. So if {{vm.class}}
is equal to Project
I'd want the ng-click
to call createProject()
. I'm not sure what is the best way to get this to happen. This is essentially what I'd want to happen though I know this doesn't work:
data-ng-click=vm.create{{vm.class}}()
which would call createGroup()
or createProject()
EDIT:
So something like:
data-ng-click="create(vm.class)"
and then:
function create(class){
switch(class){
case Group
createGroup();
}
etc....
}
Upvotes: 3
Views: 64
Reputation: 13381
So something like:
data-ng-click="create(vm.class)"
You can use right like this if add create
method to $scope
or with a bit modification:
data-ng-click="vm.create(vm.class)"
and
vm.create = function create(class){
switch(class){
case Group
createGroup();
}
etc....
}
OR even without common create function like this
data-ng-click="vm['create'+vm.class]()"
Upvotes: 0
Reputation: 5357
I would suggest keeping the logic in the controller. That is, ngClick
should call a general function in the controller, that will decide which function to call based on other parameters.
HTML:
<any ng-click="vm.create()"></any>
Controller:
vm.create = function() {
switch(vm.class) {
//logic
}
}
Upvotes: 3