3

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?

1
  • Which id do you need to send via click? If it is dynamic, then you will have to bind it within the ng-repeat itself. Else you could pass it as static value. Commented Apr 28, 2016 at 12:59

2 Answers 2

1

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)"

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

1 Comment

this is fully correct. in fact, events dealing with states preserved on controller scope have little need of receive arguments.
0

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>

3 Comments

I know forEach, but what's the this for?
@Jennifer It is thisArg parameter, It is provided to forEach(), it will be passed to callback when invoked, for use as its this value.

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.