Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding parameter to ng-click function inside ng-repeat doesn't seem to work

I have a simple loop with ng-repeat like this:

<li ng-repeat='task in tasks'>   <p> {{task.name}}   <button ng-click="removeTask({{task.id}})">remove</button> </li> 

There is a function in the controller $scope.removeTask(taskID).

As far as I know Angular will first render the view and replace interpolated {{task.id}} with a number, and then, on click event, will evaluate ng-click string.

In this case ng-click gets totally what is expected, ie: ng-click="removeTask(5)". However... it's not doing anything.

Of course I can write a code to get task.id from the $tasks array or even the DOM, but this does not seem like the Angular way.

So, how can one add dynamic content to ng-click directive inside a ng-repeat loop?

like image 636
Paweł Szymański Avatar asked Jun 11 '13 08:06

Paweł Szymański


People also ask

Can we put condition in NG-click?

We can add ng-click event conditionally without using disabled class.

How do I get the index of an element in NG-repeat?

Note: The $index variable is used to get the Index of the Row created by ng-repeat directive. Each row of the HTML Table consists of a Button which has been assigned ng-click directive. The $index variable is passed as parameter to the GetRowIndex function.

Can we use NG-click and Onclick together?

For a single btn, it's ok to use ng-click or onclick in the ng-app . There is no difference between the two functions. For effective team work, you,d better to have an account with each other. In Angular apps, ng-click is recommended.

How does ng-repeat work?

The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object. Note: Each instance of the repetition is given its own scope, which consist of the current item.


1 Answers

Instead of

<button ng-click="removeTask({{task.id}})">remove</button> 

do this:

<button ng-click="removeTask(task.id)">remove</button> 

Please see this fiddle:

http://jsfiddle.net/JSWorld/Hp4W7/34/

like image 57
Chubby Boy Avatar answered Sep 21 '22 07:09

Chubby Boy