VolcovMeter VolcovMeter - 4 months ago 36
AngularJS Question

AngularJS: How to pass in multiple INT values as string from ng-click?

Say for example I have an element like this:

<input type="button" id="model{{id}}{{Value}}" ng-click="myFunction( )">


if
id
was 178 and
Value
was 123, how can I make the
ng-click
above concatenate those value and pass them into the function?

Simply by going
ng-click=myFunction(id + Value)
would return a sum, when I would want the value passed into that function to be 178123.

This possible?

Answer

Simple, use the Number.prototype.toString() method:

<input type="button" id="model{{id}}{{Value}}" ng-click="myFunction(id.toString() + Value.toString())">

Simple demo:

(function() {
  angular
    .module('app', [])
    .controller('mainCtrl', mainCtrl);

  function mainCtrl($scope) {
    $scope.id = 178;
    $scope.Value = 123;
    
    $scope.myFunction = function(value) {
      console.log(value);
    }
  }
})();
<!DOCTYPE html>
<html ng-app="app">

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
</head>

<body ng-controller="mainCtrl">
  <input type="button" id="model{{id}}{{Value}}" ng-click="myFunction(id.toString() + Value.toString())" value="Click">
</body>

</html>