Jennifer
Jennifer

Reputation: 905

Pass array into ng-click

http://jsfiddle.net/u0jzkye1/

<div ng-app ng-controller="MyCtrl">
    <ul>
        <li ng-repeat="item in items">{{item.name}}</li>
    </ul>
    <button type="submit" ng-click="send()">
    Send
    </button>
</div>

When the button is not within ng-repeat, how can I pass my item's id into ng-click function?

Upvotes: 3

Views: 2233

Answers (2)

0xdw
0xdw

Reputation: 3842

var items = [{
  id: 1,
  name: "one"
}, {
  id: 2,
  name: "two"
}];

function MyCtrl($scope) {
  $scope.items = items;

  $scope.send = function() {
    //get items' id
    $scope.items.forEach(function(item){
      alert(item.id);
    }, this);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="MyCtrl">
  <ul>
    <li ng-repeat="item in items">{{item.name}}</li>
  </ul>
  <button type="submit" ng-click="send()">
    Send
  </button>
</div>

Upvotes: 0

juco
juco

Reputation: 6342

Well items is already on $scope as you're using it in your ng-repeat. So you should simply be able to ng-click="send(items)"

Upvotes: 1

Related Questions