4

I am just asking this as I cant find it anywhere.

I have an angular object column.addOnParams

This object containes a child element of rowClick, so it would be column.addOnParams.rowClick

In this child element contains the word myclickevent();

Is it possible to pass this to a ng-click.

So it would be as follows.

<td ng-repeat="column in columns" class="" ng-click="column.addOnParams.rowClick"></td>

I have tried it in the way above, but nothing gets generated, it just shows it as it is, after page load.

I also tried the following,

<td ng-repeat="column in columns" class="" ng-click="{{column.addOnParams.rowClick}}"></td>

That throws a reference error in angular,

Syntax Error: Token 'column.addOnParams.rowClick' is unexpected, expecting [:] at column 3 of the expression [{{column.addOnParams.rowClick}}] starting at [column.addOnParams.rowClick}}].

Any advice from you guys?

Side note when using this for the class object it works.

<td ng-repeat="column in columns" class="{{column.addOnParams.cssClass}}"></td>

The above statement generates and works perfectly.

Thanks to @J-D comment, the plunker solution worked like a charm Working solution

Using the this keyword was what I needed.

4
  • Try this column.addOnParams[rowClick](). The rowClick should be a string without () , like myclickevent Commented Oct 8, 2015 at 5:56
  • ng-click="column.addOnParams.rowClick()" ... gets bound directly to controller method Commented Oct 8, 2015 at 5:58
  • Both of this different case will solve your problem. plnkr.co/edit/aWIS1oHja11hGm4pycMC?p=preview and jsfiddle.net/nkuDW/1 Commented Oct 8, 2015 at 6:40
  • Thanks @J-D your solution worked./ Commented Oct 8, 2015 at 7:06

2 Answers 2

3

You can use $eval to call myclickevent().

Example given below:

In your controller

$scope.myclickevent = function(rowClick) {
   //your functionality here.
}

and suppose you have myclickevent() string in column.addOnParams.rowClick

Then In your html template

<td ng-repeat="column in columns" class="" ng-click="$eval(column.addOnParams.rowClick)"></td>

Hope it would help you.

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

3 Comments

Tried your solution, but it does nothing at all, really hoped it would work, but no function gets called, and no error is thrown either. So I guess that $eval is not working in this case.
Please have a look on jsfiddle jsfiddle.net/U3pVM/19266 Here addTodo function is calling by callAddTodo.
Thanks @Praveen your solution also worked, but had to tinker with some of the variables I had.
3
<td ng-repeat="column in columns" class="" ng-click="{{column.addOnParams.rowClick}}"></td>

Above code will not work. ng-click is used for calling function when user click on it. You can pass parameter to function. So you updated code will look like:

<td ng-repeat="column in columns" class="" ng-click="myClickFunction(column.addOnParams.rowClick)></td>

And in your controller, myClickFunction bind to scope.

$scope.myClickFunction = function(rowClick) {
   //do whatever you want to do
}

Based on your code remove {{}} then it will work fine:

<td ng-repeat="column in columns" class="" ng-click="column.addOnParams.rowClick"></td>

2 Comments

Your solution is working, but this is not what I am after, I don't want a myClickFuntion(), the child element should specify what the functions name is, and that should be called. As the function could be different for each column created. @SarjanDesai
That's why based on your code, check last line of answer. Remove {{}} from ng-click and you get function name from rowClick

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.