Cris Cris - 8 days ago 6
AngularJS Question

Angular attach extra values to a clicked link

Is there any way i could attach the values from the linksList( for ex: gameID) and get them in the click event? Or is my whole approach totally wrong?

HTML

<ul ng-repeat="item in linksList">
<li ng-click="onClickLink($event)">
<a href="#">{{ item.gameName }}</a></li>
</ul>


JS

.controller('LinkCtrl', function ($scope) {
$scope.linksList = [
{
gameID: 'l2',
gameName: 'Lineage 2'
},
{
gameID: 'h4',
gameName: 'Heroes of Might and Magic IV'
}
]

$scope.onClickLink = function ($event) {
console.log($event.target.text)
}
}

Answer

You may use something like this.

<ul ng-repeat="item in linksList">
  <li ng-click="onClickLink(item.gameID)">
   <a href="#">{{ item.gameName }}</a></li>
</ul>

And inside your controller

$scope.onClickLink = function (gameID) {
    console.log(gameID)
}