4

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

2 Answers 2

9

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"}
  };
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can, but I think you are mixing some things up. In your code, you are setting a textVar variable to either 1 or 2. This variable is never used however.

You'll have to bind the element to a variable on the scope, and then in your update function update the relevant scope variable, for instance:

$scope.update = function(button, text) {
  if(button == 1) {
    $scope.text1 = text;
  } else if(button == 2) {
    $scope.text2 = text;
  }
}

And your HTML:

<button ng-click="update(1,'Text for text1');">update text 1</button>
<button ng-click="update(2, 'Text for text2');">update text 2</button>
<span ng-bind="text1"></span>, <span ng-bind="text2"></span>

Comments

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.