Variable to find items in array

I am assigning a variable (ex. var
choice=“book1”) and then using it to find items in an array called
listofbooks…so in my html I have {{listofbooks.choice[0].title}} or
{{listofbooks.choice[0].price}} but it is not coming up with
anything…

BTW {{listofbooks.book1[0].title}} works

if you want to access the variable in the template you need to put it on the scope:

$scope.choice = “book1”

and you can not concat variables in object-calls.
you are trying to get “{listofbooks: { choice: […] }}” if you write this “listofbooks.choice[0].price”

change it to “listofbooks[choice][0].price”

This is the solution I was given on another forum but I can’t access choice from another controller, do you have any suggestions? (I tried changing $scope. to $rootScope but still no luck)

  $scope.allbooks = {
      book1: {
        title: "Eat Pray Love",
        price: "$3.99"
      },
      book2: {
        title: "DaVinci Code",
        price: "$7.99"
      }
    };

    $scope.choice = {}; //a variable to store your choice

    $scope.pick = function(name) {
      $scope.choice = $scope.allbooks[name];
    }