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

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

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

    
asked by anonymous 07.05.2018 / 14:19

2 answers

1

It's just separated with a comma like this:

deleteCategory(cat.id, cat.name)

Getting this way:

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

Remembering that your deleteCategory function must be changed to get both values.

If you want to perform two functions, you only have to call both of them within 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><bodyng-app="myApp">
<div ng-controller="myCtrl">
  <button ng-click="somar(2, 5)">OK</button>
  <p>Resultado: {{count}} </p>
</div>
</body>
    
07.05.2018 / 14:26
1
  

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

Yes, it is possible. However, your event model might be even more resilient if you pass the 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);
}
    
07.05.2018 / 14:56