0

I have the following function

 $scope.makeBasic = function (item, node1, node2, node3) {

  var rabbitHole = $scope[dbTable][node1][node2][node3];
  ...
}

in the html i call it like this

ng-change="makeBasic(item, 'contactData', 'emails', 'data')"

dbTable = "people" which comes from a different var and is working ok.

the end goal for the $scope is this:

$scope.people.contactData.emails.data

I can construct the scope fine with the rabbitHole variable but that restricts me to only to the 3 variables node1, node2, node3

I am trying to get to the desired $scope with this:

ng-change="makeBasic(item,'contactData.emails.data')"  

 $scope.makeBasic = function (item, sample) {

var rabbitHole = $scope[dbTable][sample];
 ...
}

but is not working and is undefined.

this way what ever i put in the var sample ('contactData.emails') or ('contactData.emails.data...') etc the $scope can be constructed accordingly

does anyone know what's the solution?

Sev
  • 15,401
  • 9
  • 56
  • 75
user2062455
  • 411
  • 5
  • 14
  • not exactly sure what your expected is but, if you want a xpath with the content made from arguments you can do $scope.rabbitHole = [dbTable][node1][node2][node3]; now this path is exposed to the view for use. – alphapilgrim Feb 22 '17 at 20:11
  • i want to avoid [node1][node2][node3], and just have[sample], which will include the arguments. does that make sense – user2062455 Feb 22 '17 at 20:15
  • you might have to do string manipulation splice and join at the '.', alternatively i suggest using [lodash-get method](https://lodash.com/docs/3.10.1#get). you could even look at the source code to see how they implement this that way you wouldn't have to add this utility lib. – alphapilgrim Feb 22 '17 at 20:19

0 Answers0