Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make ng-click call a certain method depending on a variable

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....
}
like image 393
erp Avatar asked Apr 28 '26 12:04

erp


1 Answers

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
  }
}
like image 68
Yaron Schwimmer Avatar answered May 01 '26 02:05

Yaron Schwimmer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!