How do you pass two parameters in Ng-click Angular JS?

Asked

Viewed 1,034 times

0

 <button class="btn btn-conf-t btn-deletar" ng-click="deleteCategory(cat.id)"><i class="fa fa-trash"></i></button>

Here I just step one parameter, it is possible to pass ng-click="deleteCategory(cat.id, cat.nome)" ?

2 answers

1

It just separates with a comma, this way:

deleteCategory(cat.id, cat.name)

Thus remaining:

<button class="btn btn-conf-t btn-deletar" ng-click="deleteCategory(cat.id, cat.name)"><i class="fa fa-trash"></i></button>

Remembering that its function deleteCategory should be changed to receive both values.

If you want to perform two functions just call the two inside deleteCategory.

See a small example:

angular.module('myApp', [])
.controller('myCtrl', ['$scope', function($scope) {
  $scope.count = 0;
  $scope.somar = function(val, val2) {
    $scope.count = val + val2;
  };
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
<div ng-controller="myCtrl">
  <button ng-click="somar(2, 5)">OK</button>
  <p>Resultado: {{count}} </p>
</div>
</body>

1

...it is possible to pass ng-click="deleteCategory(cat.id, cat.name)"?

Yes, and possible. However, your event model might be even more resistant if you go through object reference:

ng-click="deleteCategory(cat)"

Within your controller you can then read the properties of the referenced object:

$scope.deleteCategory = function (cat) {
    console.log(cat.id, cat.nome);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.