1

I am still having trouble using the var to identify the object in the array. Here is what I have right now:

in controller:

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

$scope.pick=function(name){
     $rootScope.choice = name;
}

in html template

<ion-item ng-click="pick(book1)">

{{allbooks.choice[0].title}} <----This does not work

Used an alert to make sure choice=book1 which it does...I don't know what I am doing wrong

PLEASE HELP :(

1
  • 1
    Your allbooks assignment statement does not look like proper Javascript syntax. Commented Dec 3, 2015 at 4:04

2 Answers 2

1
[
{book1={title:"Eat Pray Love", price:"$3.99"},
{book2={title:"DaVinci Code", price:"$7.99"}}
]

is not valid javascript. If you are trying to define an object literal you'll need to adjust your syntax a bit:

$scope.allbooks = [
    {title:"Eat Pray Love", price:"$3.99"},
    {title:"DaVinci Code", price:"$7.99"}
]

Now you can access each book:

$scope.allbooks[0]; // returns first book
$scope.allbooks[1]; // returns second book
Sign up to request clarification or add additional context in comments.

3 Comments

I totally get what your saying about the syntax, I think I just typed it in too fast...but how do I fix this issue {{allbooks.choice[0].title}}
{{ allbooks[0].title }} ?
I will try to explain my issue again...so i want to access this part of an array: {{things.book1.price}} but instead the user chooses which books price to view...so they can pick from any book from book1 to book99...so I need to replace {{things.book1.price}} with {{things.choice.price}} where choice will be assigned based on user choice
1

in controller:

$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];  
}

in html template

<ion-item ng-click="pick('book1')"> <!-- pass book1 as string -->

{{choice.title}}


angular.module("app", [])
  .controller("main", function($scope) {

    $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];
    }

  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="main">

  <button ng-click="pick('book1')">Test</button>
  <!-- pass book1 as string -->

  {{choice.title}}

</div>

15 Comments

missed a '}' for book1
I tried but it didn't work...if i just use {{allbooks.book1.title}} it works but as soon as i try to dynamically replace it with the variable it falls apart :(
have you defined $scope.choice variable in your scope?
I am using $rootScope.choice instead of $scope.choice so it should be available to all the scopes...(but I might not be correct)
button does not work anymore...i had to change <ion-item ng-click="pick('book1')"> to <ion-item ng-click=pick('book1')> but it is still not working....whats crazy is that I was able to get {{choice}} to work and it showed book1 in the html file...this makes no sense
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.