Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I'm new to angularJS and can't seem to figure out how to parse multiple variables from a html button thru an angular JS function.

<html ng-app="">

<head>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
</head>

  <body ng-controller='CartController'>

    <script>
      function CartController($scope) {
        $scope.addToCart = function(index,product) {
          alert(product);
        };
      }
    </script>

    <button ng-click="addToCart(2,iPhone);">Add to cart</button>

  </body>

</html>

I would expect the second variable (iPhone) would get stored in variable named product but the alert shows its undefined.

What am I doing wrong?

share|improve this question
up vote 0 down vote accepted

Try using ng-click="addToCart(2,'iPhone');"

What you want is to pass the string 'iPhone', not a property on the $scope called iPhone.

Without the quotes, it will be evaluated against the $scope in CartController. There is no iPhone property on $scope so it will be undefined.

Your original try would work if in CartController you'd have:

$scope.iPhone = "Hello";

The string Hello would be passed to the addToCart function.

share|improve this answer
    
Thanks Szabolcs, this solved the problem. Now that I know it was a bit obvious. – RvdM 54 mins ago
    
You're welcome. – Szabolcs Dézsi 53 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.