Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing parameters to ng-click's function in angularjs

How to pass, so to say, refference to scope's parameter, in ng-click You can check what I want in this pluner

http://plnkr.co/edit/Em7LiNStICjZA23pSph3?p=preview

like image 529
Midnight Guest Avatar asked Dec 25 '22 11:12

Midnight Guest


1 Answers

Typically, you wouldn't need to do this dynamically as you would have specific $scope properties on inputs etc.

Here is an updated plunker.

HTML:

<div class="test" ng-controller="Ctrl">
   <button ng-click="update(1, 'text1');">update text 1</button>
   <button ng-click="update(2, 'text2');">update text 2</button>
    {{text1}} , {{text2}}
<div>

JS:

function Ctrl($scope) {
  $scope.update = function(parameter1, textVar){
      if (parameter1 === 1) {$scope[textVar] = "1"}
      if (parameter1 === 2) {$scope[textVar] = "2"}
  };
}
like image 146
Davin Tryon Avatar answered Dec 28 '22 06:12

Davin Tryon