0

I would like to find out how I can pass a parameter that was dynamically created using ng-repeat back to the controller by calling a controller function.

Currently when I call showItem('{{item.item_id}}'); I see {{item.item_id}} in my controller function. If I leave the parenthesis off, it seems like the function is not being called (no output in console.log).

$scope.showItem = function(itemId) {
		console.log('show item '+itemId);
	}
<ul ng-repeat="items in data">
  <li>
    <a ng-click="showItem('{{items.item_id}}')" href="javascript:void(0);" >
      {{items.name}}
    </a>
  </li>	
</ul>

2 Answers 2

2
<a ng-click="showItem(item.item_id)" href="">

Note that an empty href is all you need to prevent navigation.

Also note that you could have found that by looking at the documentation, which explains what ng-click expects (an expression), and has examples.

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

2 Comments

I love your answers. Always follow your answer as learning tip for angularJS.
I tried to make the samething here is my example. Simplified code. <div data-ng-repeat="indicator in competence.indicators" class="content level2"> <a ng-click="listAllRestulatsByIndicateurId(indicator.id)" href="javascript:void(0);">Click me</a> </div> But my HTML do not write the correct parameter in the function. I tried to show it with {{indicator.id}} and it worked.
1

Just pass it without interpolation:

<ul ng-repeat="item in data">
  <li>
    <a ng-click="showItem(item.item_id)" href="javascript:void(0);">
      {{item.name}}
    </a>
  </li> 
</ul>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.