0

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

Community
  • 1
  • 1
sam
  • 3
  • 1
  • 4
  • 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 http://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
  • @sam, also i'm sure you not need save id for textbox, combobox and other html component, can you provide sampl problem what you try solve? – Grundy May 26 '15 at 12:29
  • i guess @brocco 's answer going to solve my problem.First i was using 'id' to get my value using jquery but after started to use angular,this problem arised. But i guess it solved now. Thanks grundy for your help and consideration. – sam May 26 '15 at 12:34

2 Answers2

1

This will get the value for you dynamically:

var getMyValue = $scope[$scope.myarray[0].id];
Brocco
  • 62,737
  • 12
  • 70
  • 76
  • 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
0

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);

});
User2
  • 1,293
  • 8
  • 17