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 have array of object which contains number of scope variable name in it. All the scope variable value has already been set before.

Something like this :

$scope.myarray = [{'id':'myname1'},{'id':'myname2'}];
$scope.myname1 = 'John';
$scope.myname2 = 'Rick';

Now if I want to get value of the scope variable which within the 'id' of 'myarray',what should I do?
I have already tried this

var getMeMyValue = $scope[myarray[0]];

Something like this,but it didnt help.
I have seen in this example that how to set scope variable dynamically
But I didnt get anything about how to get value dynamically

Please help me with this,Thanks!!

P.S. Here I'm dynamically getting my scope variable so there is no way that I can access them directly to get their value

share|improve this question
    
it work $scope.myarray = ['myname1','myname2']; var getMeMyValue = $scope[myarray[0]]; – Grundy May 26 '15 at 11:57
    
can you show working sample how you try, and what error you have? – Grundy May 26 '15 at 11:58
    
Or can you please create an sample in jsfiddle.net? – Arasu RRK May 26 '15 at 12:01
    
Thanks @grundyGrundy, for consideration. Actually i have scope variable that assigned to several html component (textbox,combobox,etc) as per they created dynamically and I set the id same as the scope variable. I also stored these variables in a array above shown manner. – sam May 26 '15 at 12:16
    
can you provide jsfiddle or plunker? now not quite clear where is problem in your code – Grundy May 26 '15 at 12:18
up vote 0 down vote accepted

This will get the value for you dynamically:

var getMyValue = $scope[$scope.myarray[0].id];
share|improve this answer
    
I think this will solve my problem. Thanks @brocco and sorry that i cant gives you thumbs up..not enough reputation :P – sam May 26 '15 at 12:39
    
No problem, if this does solve your problem, please mark it as the accepted answer (even if you cannot upvote due to rep) – Brocco May 26 '15 at 12:52

Please check this http://plnkr.co/edit/ZwwuKRVwgD74ufY2GmB8?p=preview

Controller -

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

app.controller('myController', function($scope) {
  $scope.myarray = [{'id':'myname1'},{'id':'myname2'}];
  $scope.myname1 = "John;"
  var getMeMyValue = $scope[$scope.myarray[0].id];
  console.log(getMeMyValue);

});
share|improve this answer
    
Is it working for you? – User2 May 26 '15 at 12:26
    
your answer same as @Brocco answer – Grundy May 26 '15 at 12:27
    
Ohh yes.. I have not checked. – User2 May 26 '15 at 12:30

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.