Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in Web Technology by (47.6k points)

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?

1 Answer

+1 vote
by (106k points)

You can get it to work by using the below-mentioned code:-

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

Or you can also do this:

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

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...