Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

My code is something like this

In View HTML :

<div ng-app="myApp" ng-controller="myCtrl"> 
  <button ng-click="get_user_detail(Andrew)"> 
  </button>

  <button ng-click="get_user_detail(Andrew1)"> 
  </button>
</div>

Angular JS :

   var app = angular.module("myApp", []);

   app.controller("myCtrl", ['$compile', '$http', '$scope', function ($compile, $http, $scope) {
     $scope.get_user_detail=function(name){
         var response = $http.get("myURL", {
            params: {user_id:12345,name:name}
        });
      response.success();
      response.error();
     }
 }]);

While I was working with one parameter user_id,it was working fine,parameters were passed properly to request.After I added 2nd parameter name,it is not getting passed in params.

share|improve this question
    
What is Andrew and Andrew1? input? Scope's element? – Kamuran Sönecek 9 hours ago
    
Try putting name in quotations like 'Andrew'. Otherwise it will look for key Andrew in scope. – Harish Kommuri 9 hours ago
    
Can you show us how you were trying to pass a second parameter. – phuzi 9 hours ago
    
Andrew and Andrew 1 are static string values ,Tried with quotation also. – Groovy 9 hours ago
1  
your code should work fine if you add " " to the names, look at this plunkr I made with your code. – Jax 9 hours ago

Andrew is not a variable of your $scope.

So when you do get_user_detail(Andrew), the value of Andrew is undefined.


I guess you would like to pass it as a static value (string), put your value between quotes ' ':

<button ng-click="get_user_detail('Andrew')"> 
share|improve this answer
    
Tried not working. – Groovy 9 hours ago
    
@Groovy Could you explain what not working means? What is the behavior? Do you have any error? – Mistalis 9 hours ago
    
Not any error. Not getting value of variable 'name' in function and it is not getting passed in params. – Groovy 9 hours ago

As in the other answers, your code should work as long as you add " " to the strings you intend to pass as params.

var app = angular.module('ngApp', []);

app.controller("myCtrl", ['$compile', '$http', '$scope', function ($compile, $http, $scope) {        
     
  $scope.get_user_detail = function(name){
     $scope.name = name
     // response = $http.get("myURL", {
     //     params: {user_id:12345,name:name}
     // });
     // response.success();
     // response.error();
   }
 }]); 
<div ng-app="ngApp" ng-controller="myCtrl">
  You clicked {{name}} <br/>
  <button ng-click="get_user_detail('Andrew')"> Andrew
  </button>

  <button ng-click="get_user_detail('Bob')"> Bob
  </button>

  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
  <script src="app.js"></script>

</div>

I commented the $http call for obvious reasons. Hope it helps.

share|improve this answer

You must have string in ng-click in ' ':

<button ng-click="get_user_detail('Andrew')"> 

and in js code - name put in ' '

params: {user_id:12345,'name':name}
share|improve this answer

I thought you not define variables in controller, define variable with $scope

<div ng-app="myApp" ng-controller="myCtrl"> 
  <button ng-click="get_user_detail(Andrew)"> 
  </button>

  <button ng-click="get_user_detail(Andrew1)"> 
  </button>
</div>

Controller Code

var app = angular.module("myApp", []);

   app.controller("myCtrl", ['$compile', '$http', '$scope', function    ($compile, $http, $scope) {
     $scope.Andrew = 'John';
     $scope.Andrew = 'Jam';
     $scope.get_user_detail=function(name){
         var response = $http.get("myURL", {
            params: {user_id:12345,name:name}
        });
      response.success();
      response.error();
     }
 }]);

Other soluation you can direct pass string without define variable

<div ng-app="myApp" ng-controller="myCtrl"> 
  <button ng-click="get_user_detail('Andrew')"> 
  </button>

  <button ng-click="get_user_detail('Andrew1')"> 
  </button>
</div>
share|improve this answer

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.