checkmate711
checkmate711

Reputation: 3461

Angular: pass dynamic parameter to controller

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>

Upvotes: 0

Views: 1932

Answers (2)

Phuong Nguyen
Phuong Nguyen

Reputation: 3070

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>

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691645

<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.

Upvotes: 2

Related Questions