2

I am trying to pass a string to a function through ng click, here's an example

HTML

<a class="btn"> ng-click="test(STRING_TO_PASS)"</a>

Controller

$scope.test = function(stringOne, stringTwo){ valueOne = test(STRING_TO_PASS); }

Edit -

Turns out I was thinking about this the wrong way... not really sure why I asked it, sorry guys!

4
  • Is there a question here? Commented Dec 4, 2017 at 13:08
  • What is the problem? test function is undefined in your controller, have you defined it anywhere else! you should use ng-click like this: 'test("STRING_TO_PASS")', if its not a angular variable. Commented Dec 4, 2017 at 13:08
  • in your controller it would be: $scope.test = function(STRING_TO_PASS), what kind of behaviour / logic are you looking for? Commented Dec 4, 2017 at 13:11
  • Edited question. Sorry guys! Commented Dec 4, 2017 at 13:13

3 Answers 3

3

Your controller function should accept only 1 parameter

$scope.test = function(stringOne){
    valueOne =  stringOne;
}
Sign up to request clarification or add additional context in comments.

1 Comment

i think now you should be able to
2

Correct HTML for ng-click to be used as an attribute is as

<a class="btn" ng-click="test('STRING_TO_PASS')"> Click</a>

Also, the test function would accept one parameter in your controller.

var app = angular.module("app", []).controller("ctrl", function($scope) {
$scope.test = function(stringOne) {
    valueOne = stringOne;
    console.log(valueOne);
  };
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="app" ng-controller="ctrl">
  <a class="btn" ng-click="test('STRING_TO_PASS')"> Click</a>
</body>

Comments

0

I think you want something like this.

HTML

<a class="btn"> ng-click="test("StringOne","StringTwo")"</a>

Controller

$scope.test = function(stringOne, stringTwo){
    var valueOne = stringOne;
var valueTwo = stringTwo
}

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.