Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

pardon me if this is a repeat question and i am still a beginner.

see the code below :

$scope.clicked=function(key,value){
    $scope[key]=value;}

the above code returns $scope.key is undefined or $scope.key=value even if i pass any value to key. lets say i call the function using $scope.clicked('yes','i am yes') . I want $scope.yes = 'i am yes'; what i get is $scope.keys='i am yes'; . how do i achieve what i want.?

share|improve this question
1  
$scope[key]=value; should work. –  BatScream 18 hours ago
1  
$scope.[key]=value is invalid syntax. Try $scope[key]=value –  Ed Cottrell 18 hours ago
    
sorry about that. i was using right syntax in my original code. here i typed wrong. –  raj 18 hours ago
    
$scope.keys='i am yes'; ,is it incorrect too, as there is no variable like keys. And please copy paste the exact code which you are working on. –  Rishi Prakash 18 hours ago
    
Although it's working I have checked it on Jsfiddle. –  Rishi Prakash 18 hours ago

1 Answer 1

$scope.clicked=function(key,value){
    $scope[key]=value;}

You are using incorrect Syntax $scope.[key] is wrong. $scope[key] is right.

share|improve this answer
    
sorry about that. i was using right syntax in my original code. here i typed wrong. –  raj 18 hours 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.