0

I have a new case, where I need help. I want to have 9 buttons and a panel that displays the movie details, depending on which one has been clicked. So say if I clicked 'Transformers', the transformers detail should appear in the panel. Then if I were to click 'Fury', the panel detail would change to the Fury details. I need this data to be in a JSON file. I've looked, how to do this and struggle to understand how I would go about doing this. Here's what I've got so far.

JS:

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

app.controller('MainController', ['$scope', function ($scope) {


}])

JSON:

{
  "movie": [
  {
    "id": 1,
    "name": "Star Wars The Phantom Menace",
    "format": "DVD",
    "rate": 4,
    "price": 2
  },
  {
    "id": 2,
    "name": "Star Wars A New Hope",
    "format": "DVD",
    "rate": 6,
    "price": 4
  },
  {
    "id": 3,
    "name": "Transformers",
    "format": "Blu-Ray",
    "rate": 7,
    "price": 3
  },
  {
    "id": 4,
    "name": "Transformers Dark of the Moon",
    "format": "Blu-Ray",
    "rate": 6,
    "price": 5
  }
]}

HTML:

  <body ng-controller="MainController" ng-app="myApp">
    <h1 style="text-align:center">Garrett's Rentals</h1>

    <div ng-repeat="movie in movies">
      <button>{{movie.name}}</button>
    </div>

    <div class="panel">
      <h3>You have selected:</h3>
      <p>{{movie.name}}</p>
      <p>{{movie.format}}</p>
      <p>{{movie.rate}}</p>
      <p>{{movie.price}}</p>
    </div>
  </body>

6 Answers 6

2

use

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

app.controller('MainController', ['$scope', function ($scope) {
    var json={
  "movie": [
  {
    "id": 1,
    "name": "Star Wars The Phantom Menace",
    "format": "DVD",
    "rate": 4,
    "price": 2
  },
  {
    "id": 2,
    "name": "Star Wars A New Hope",
    "format": "DVD",
    "rate": 6,
    "price": 4
  },
  {
    "id": 3,
    "name": "Transformers",
    "format": "Blu-Ray",
    "rate": 7,
    "price": 3
  },
  {
    "id": 4,
    "name": "Transformers Dark of the Moon",
    "format": "Blu-Ray",
    "rate": 6,
    "price": 5
  }
  ]};
console.log(json);
$scope.movies=json.movie;
    console.log($scope.movie);
}]);

HTML

    <body ng-controller="MainController" ng-app="myApp">
    <h1 style="text-align:center">Garrett's Rentals</h1>

    <div ng-repeat="movie in movies">
      <button>{{movie.name}}</button>
    </div>

    <div class="panel">
      <h3>You have selected:</h3>
      <p>{{movie.name}}</p>
      <p>{{movie.format}}</p>
      <p>{{movie.rate}}</p>
      <p>{{movie.price}}</p>
    </div>
  </body>

Fiddle for support:http://jsfiddle.net/sXkjc/993/

Sign up to request clarification or add additional context in comments.

8 Comments

This didn't work, just returned {{movies.movie.name}}
Please recheck answer I hav edited and provided a new fiddle for solution
Thank you, the ng-repeat works on this but it doesn't display the movies data when the button is clicked?
I'm very happy! How would I do this if I were to have the JSON a seprate file?
you can use the JSON.parse() its a javascript method that will convert into a object
|
1

Use ng-click directive on button to set current selected movie like follwing.

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

app.controller('MainController', ['$scope', function ($scope) {
   $scope.movies= movies; //here movies is your json object
   $scope.selectedMovie=$scope.movies[0]; // assume that first movie is selected default
}])

HTML

<div ng-repeat="movie in movies">
  <button ng-click="selectedMovie=movie">{{movie.name}}</button>
</div>

<div class="panel">
  <h3>You have selected:</h3>
  <p>{{selectedMovie.name}}</p>
  <p>{{selectedMovie.format}}</p>
  <p>{{selectedMovie.rate}}</p>
  <p>{{selectedMovie.price}}</p>
</div>

Comments

0

JS

angular.module('myApp', [])

.controller('MainController',['$scope','$http',function($scope,$http){


  $scope.contents=null;
  $http.get('URL TO JSON').then(function(resp){
    console.log('Success', resp);
    $scope.contents=resp.data;


  },
  function(err){
    console.error('ERR',err);
  })

}]);

HTML

 <body ng-controller="MainController" ng-app="myApp">
<h1 style="text-align:center">Garrett's Rentals</h1>

<div ng-repeat="movie in movies">
  <button>{{movie.name}}</button>
</div>

<div class="panel">
  <h3>You have selected:</h3>
  <p>{{movie.name}}</p>
  <p>{{movie.format}}</p>
  <p>{{movie.rate}}</p>
  <p>{{movie.price}}</p>
</div>

2 Comments

How would I then call the data in the html?
you can use ng-click=function({{movie.name}}) in button tag.you can define a function in your controller which selects the corrsponding movie then
0

Controller Code:

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

    app.controller('MainController', ['$scope', function ($scope) {

        $scope.movies = {

            movie: [
            {
                id: 1,
                name: "Star Wars The Phantom Menace",
                format: "DVD",
                rate: 4,
                price: 2
            },
            {
                id: 2,
                name: "Star Wars A New Hope",
                format: "DVD",
                rate: 6,
                price: 4
            },
            {
                id: 3,
                name: "Transformers",
                format: "Blu-Ray",
                rate: 7,
                price: 3
            },
            {
                id: 4,
                name: "Transformers Dark of the Moon",
                format: "Blu-Ray",
                rate: 6,
                price: 5
            }
            ]}

            $scope.setMovie = function(movie) {
            $scope.currentMovie = movie;
        }

    }])

HTML:

<html ng-app="myApp">

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
    <script src="app.js"></script>
  </head>


     <body ng-controller="MainController" >
    <h1 style="text-align:center">Garrett's Rentals</h1>

    <div ng-repeat="movie in movies.movie">
      <button ng-click = "setMovie(movie)">{{movie.name}}</button>
    </div>

    <div class="panel">
      <h3>You have selected:</h3>
      <p>{{currentMovie.name}}</p>
      <p>{{currentMovie.format}}</p>
      <p>{{currentMovie.rate}}</p>
      <p>{{currentMovie.price}}</p>
    </div>
  </body>

</html>

http://plnkr.co/edit/0f5bbaS38GCIjGtCfLmy?p=preview

Comments

0

I have given a working example of your requirements see http://plnkr.co/edit/uQLqB3CfSUlETQEcpsS5?p=preview

Change the html to

<div ng-repeat="movie in movies">
      <button ng-click="selectedMovie(movie)">{{movie.name}}</button>
    </div>

    <div class="panel">
      <h3>You have selected:</h3>
      <p>{{movie.name}}</p>
      <p>{{movie.format}}</p>
      <p>{{movie.rate}}</p>
      <p>{{movie.price}}</p>
    </div>

javascript to

myApp.controller('MainController', ['$scope', function($scope) {
    var data = {
  "movie": [
  {
    "id": 1,
    "name": "Star Wars The Phantom Menace",
    "format": "DVD",
    "rate": 4,
    "price": 2
  },
  {
    "id": 2,
    "name": "Star Wars A New Hope",
    "format": "DVD",
    "rate": 6,
    "price": 4
  },
  {
    "id": 3,
    "name": "Transformers",
    "format": "Blu-Ray",
    "rate": 7,
    "price": 3
  },
  {
    "id": 4,
    "name": "Transformers Dark of the Moon",
    "format": "Blu-Ray",
    "rate": 6,
    "price": 5
  }
]};
$scope.movies = data.movie;
$scope.selectedMovie = function(movie){
  $scope.movie = movie;
}
}]);

Comments

0

All you need to do is define the movies object inside your controller in order for the movies to be displayed in your front-end. Embedding the movies JSON directly in your controller would look something like this:

app.controller('MainController', ['$scope', function ($scope) {
 $scope.movies1 = 
  [{
    "id": 1,
    "name": "Star Wars The Phantom Menace",
    "format": "DVD",
    "rate": 4,
    "price": 2
  },
  {
    "id": 2,
    "name": "Star Wars A New Hope",
    "format": "DVD",
    "rate": 6,
    "price": 4
  },
  {
    "id": 3,
    "name": "Transformers",
    "format": "Blu-Ray",
    "rate": 7,
    "price": 3
  },
  {
    "id": 4,
    "name": "Transformers Dark of the Moon",
    "format": "Blu-Ray",
    "rate": 6,
    "price": 5
  }];
}]);

Note that I have removed the movie property from inside the movies object and converted movies to an array instead, so that ng-repeat will work properly.

If instead you need to have the movies JSON as a separate file you can load that file using the $http service as @KuZon noted.

$http.get('movies.json').then(function(json) {
    $scope.movies1 = json.data.movie;
  });

In this case I have left the movie attribute inside the JSON object, and used it to select the array to store in $scope.movies1.

You can see both approaches in this Plunkr

Finally, don't forget to use ng-click on your buttons to actually select the movie. Something like the following:

<button ng-click="selectMovie1(movie)">{{movie.name}}</button>

and in your controller:

$scope.selectMovie1 = function(movie) {
   $scope.movie1 = movie
 }

3 Comments

Thanks for the help but the ng-repeats don't work on these 2 examples on Plunkr?
@TheGarrett It seems to me that Plunkr is currently having some issues because I can't even open the plunker right now. Hopefully it will be resolved soon but in the meantime I have added a few extra code samples in my answer. Perhaps these will be useful.
@TheGarrett It seems to be back online now so you can try again.

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.