2016-02-14 128 views
0

我是设置正确

var dvdApp = angular.module("dvdApp", ["ngCookies"]); 
 
dvdApp.controller("dvdController", dvdController); 
 

 
function dvdController($scope, $cookies) { 
 
    
 
    $scope.shopping_cart = []; 
 
    $scope.shopping_cart_total = 0.00; 
 
    $scope.products = DVDs; 
 
    $scope.number_items_cart = 0; 
 
    $scope.show_popover = false; 
 
    $scope.show_cart = false; 
 
    $scope.avaliability = true; 
 
    $scope.genres = ["Action", "Adventure", "Comedy", "Crime", "Drama", "Fantasy", "Horror", "Sci-Fi"]; 
 
    $scope.selected_genres = []; 
 
    $scope.popularDVDs = []; 
 
    $scope.newDVDs = []; 
 
    $scope.allDVDs = DVDs; 
 
    $scope.filter = "all"; 
 
    $scope.selected_product = {}; 
 
    
 
    //THIS WILL BE FOR STORING THE COOKIES TO PRESERVE 
 
    //THE SHOPPING CART 
 
    $scope.save_data = function(){ 
 
     var n = $scope.number_items_cart.toString(); 
 
     $cookies.put("shoppingCart", $scope.shopping_cart); 
 
     $cookies.put("cartCount", n); 
 
     console.log(n); 
 
     console.log(JSON.stringify($scope.shopping_cart)); 
 
    } 
 
    
 
    $scope.delete_data = function(){ 
 
     $cookies.remove("shoppingCart"); 
 
     $cookies.remove("cartCount"); 
 
    } 
 
    
 
    $scope.get_cookie = function(){ 
 
     var cartCookie = $cookies.get("shoppingCart"); 
 
     var countCookie = $cookies.get("cartCount"); 
 
     console.log(cartCookie); 
 
     console.log(countCookie); 
 
    } 
 
    
 
    
 
    //FILTERED BY RATING  
 
    $scope.popular_DVDs = function(){ 
 
     $scope.filter = "popular"; 
 
     $scope.products = DVDs; 
 
     $scope.temp = []; 
 
     for (var i = 0; i < $scope.products.length; i ++) { 
 
     if ($scope.products[i].rating > 8) { 
 
      $scope.temp.push($scope.products[i]); 
 
     } 
 
     } 
 
     $scope.products = $scope.temp; 
 
     $scope.popularDVDs = $scope.temp; 
 
     $scope.genre_filter(); 
 
    } 
 
    
 
    //FILTERED BY ALL DVDs 
 
    $scope.all_DVDs = function(){ 
 
     $scope.filter = "all"; 
 
     $scope.products = DVDs; 
 
     $scope.allDVDs = DVDs; 
 
     $scope.genre_filter(); 
 
    } 
 
    
 
    //FILTERED BY NEWEST DVDs 
 
    $scope.new_DVDs = function(){ 
 
     $scope.filter = "new"; 
 
     $scope.products = DVDs; 
 
     $scope.temp = []; 
 
     for(var i = 0; i < $scope.products.length; i++){ 
 
     if ($scope.products[i].releaseYear > 2000) { 
 
      $scope.temp.push($scope.products[i]); 
 
     } 
 
     } 
 
     $scope.products = $scope.temp; 
 
     $scope.newDVDs = $scope.temp; 
 
     $scope.genre_filter(); 
 
    } 
 
    
 
    //CONFIRM REQUESTED DVD 
 
    $scope.confirm_request = function() { 
 
    alert("Your request has been received. We will email you when the requested DVD is available. Thank You for your business!!"); 
 
    } 
 
    
 
    //SELECT GENRE TO FILTER BY 
 
    $scope.select_genre = function(genre){ 
 
     if ($scope.selected_genres.indexOf(genre) == -1) { 
 
     $scope.selected_genres.push(genre); 
 
     } 
 
     else{ 
 
     var idx = $scope.selected_genres.indexOf(genre); 
 
     $scope.selected_genres.splice(idx, 1); 
 
     } 
 
     $scope.genre_filter(); 
 
    } 
 
    
 
    //FILTER BY GENRE 
 
    $scope.genre_filter = function(){ 
 
     $scope.temp = []; 
 
     if ($scope.filter == "all") { 
 
     $scope.products = $scope.allDVDs; 
 
     for (var i = 0; i < $scope.products.length; i++){ 
 
      for (var x = 0; x < $scope.selected_genres.length; x ++) { 
 
       if ($scope.products[i].genre == $scope.selected_genres[x]) { 
 
        $scope.temp.push($scope.products[i]); 
 
       } 
 
      } 
 
     } 
 
     if ($scope.selected_genres.length < 1) { 
 
      $scope.temp = $scope.allDVDs; 
 
     } 
 
     } 
 
     else if ($scope.filter == "popular") { 
 
     $scope.products = $scope.popularDVDs; 
 
     for (var i = 0; i < $scope.products.length; i++){ 
 
      for (var x = 0; x < $scope.selected_genres.length; x ++) { 
 
       if ($scope.products[i].genre == $scope.selected_genres[x]) { 
 
        $scope.temp.push($scope.products[i]); 
 
       } 
 
      } 
 
     } 
 
     if ($scope.selected_genres.length < 1) { 
 
      $scope.temp = $scope.popularDVDs; 
 
     } 
 
     } 
 
     else if ($scope.filter == "new") { 
 
     $scope.products = $scope.newDVDs; 
 
     for (var i = 0; i < $scope.products.length; i++){ 
 
      for (var x = 0; x < $scope.selected_genres.length; x ++) { 
 
       if ($scope.products[i].genre == $scope.selected_genres[x]) { 
 
        $scope.temp.push($scope.products[i]); 
 
       } 
 
      } 
 
     } 
 
     if ($scope.selected_genres.length < 1) { 
 
      $scope.temp = $scope.newDVDs; 
 
     } 
 
     } 
 
     $scope.products = $scope.temp; 
 
    } 
 
    
 
    //SELECTED IMAGE FOR POPOVER 
 
    $scope.selected_image = function(selected_item){ 
 
     $scope.selected_product = selected_item; 
 
     $scope.show_popover = true; 
 
     
 
    } 
 
    
 
    $scope.sold_out = function(selected_item){ 
 
     $scope.avaliability = selected_item.available; 
 
    } 
 
    //CLOSE POPOVER 
 
    $scope.close_popover = function(){ 
 
     $scope.show_popover = false; 
 
    } 
 
    
 
    //CLOSE SHOPPING CART 
 
    $scope.close_cart = function(){ 
 
     $scope.show_cart = false; 
 
    } 
 
    
 
    //SHOW SHOPPING CART 
 
    $scope.show_shopping_cart = function(){ 
 
     $scope.show_cart = true; 
 
    } 
 
    
 
    //ADD RENTED DVD TO SHOPPING CART 
 
    $scope.rented = function(rented_product){ 
 
     $scope.rented_item = {}; 
 
     $scope.rented_item = rented_product; 
 
     
 
     $scope.number_items_cart++; 
 
     $scope.rented_item.price = "1.99"; 
 
     
 
     $scope.shopping_cart_total += 1.99; 
 
     $scope.shopping_cart.push($scope.rented_item); 
 
     $scope.close_popover(); 
 
     $scope.save_data(); 
 
     
 
     //$scope.get_cookie(); 
 
     
 
    } 
 
    //ADD PURCHASED DVD TO SHOPPING CART 
 
    $scope.purchased = function(purchased_product){ 
 
     $scope.purchased_item = {}; 
 
     $scope.purchased_item = purchased_product; 
 
     
 
     $scope.number_items_cart++; 
 
     $scope.purchased_item.price = "12.99"; 
 
     
 
     $scope.shopping_cart_total += 12.99; 
 
     $scope.shopping_cart.push($scope.purchased_item); 
 
     $scope.close_popover(); 
 
     
 
     $scope.save_data(); 
 
     $scope.get_cookie(); 
 
    } 
 
    
 
    //PLACE ORDER 
 
    $scope.place_order = function(){ 
 
     $scope.show_cart = false; 
 
     $scope.shopping_cart = []; 
 
     $scope.shopping_cart_total = 0.00; 
 
     $scope.number_items_cart = 0; 
 
     
 
     $scope.delete_data(); 
 
     
 
     alert("We have received your order. It will be held for two hours. Thank You for your business!!"); 
 
    } 
 
} 
 
    
 
var DVDs = [ 
 
     { "title":"7 Assassins", 
 
      "image":"images/7Assassins.jpg", 
 
      "description":"When gold goes missing in ancient China, royal guards entrusted with its recovery realize they are not the only people in pursuit.", 
 
      "genre":"Action", 
 
      "available":false, 
 
      "rating":"5", 
 
      "releaseYear":"2013" 
 
     }, 
 
     { "title":"Doctor Who", 
 
      "image":"images/DoctorWho.jpg", 
 
      "description":"The further adventures of the time traveling alien adventurer and his companions.", 
 
      "genre":"Adventure", 
 
      "available":false, 
 
      "rating":"8", 
 
      "releaseYear":"2005" 
 
     }, 
 
     { "title":"The Godfather", 
 
      "image":"images/Godfather.jpg", 
 
      "description":"The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son.", 
 
      "genre":"Crime", 
 
      "available":true, 
 
      "rating":"9", 
 
      "releaseYear":"1972" 
 
     }, 
 
     { "title":"The Godfather Part II", 
 
      "image":"images/GodfatherPartII.jpg", 
 
      "description":"The early life and career of Vito Corleone in 1920s New York is portrayed while his son, Michael, expands and tightens his grip on his crime syndicate stretching from Lake Tahoe, Nevada to pre-revolution 1958 Cuba.", 
 
      "genre":"Crime", 
 
      "available":true, 
 
      "rating":"9", 
 
      "releaseYear":"1974" 
 
     }, 
 
     { "title":"One Flew Over the Cuckoos Nest", 
 
      "image":"images/OneFlewOverTheCuckoosNest.jpg", 
 
      "description":"A criminal pleads insanity after getting into trouble again and once in the mental institution rebels against the oppressive nurse and rallies up the scared patients.", 
 
      "genre":"Drama", 
 
      "available":false, 
 
      "rating":"8", 
 
      "releaseYear":"1975" 
 
     }, 
 
     { "title":"Se7en", 
 
      "image":"images/Se7en.jpg", 
 
      "description":"Two detectives, a rookie and a veteran, hunt a serial killer who uses the seven deadly sins as his modus operandi.", 
 
      "genre":"Horror", 
 
      "available": true, 
 
      "rating":"8", 
 
      "releaseYear":"1995" 
 
     }, 
 
     { "title":"Seven Samurai", 
 
      "image":"images/SevenSamurai.jpg", 
 
      "description":" poor village under attack by bandits recruits seven unemployed samurai to help them defend themselves.", 
 
      "genre":"Drama", 
 
      "available":false, 
 
      "rating":"9", 
 
      "releaseYear":"1954" 
 
     }, 
 
     { "title":"The Shawshank Redemption", 
 
      "image":"images/Shawshank.jpg", 
 
      "description":"Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common decency.", 
 
      "genre":"Drama", 
 
      "available":true, 
 
      "rating":"9", 
 
      "releaseYear":"1994" 
 
     }, 
 
     { "title":"Office Space", 
 
      "image":"images/officespace.jpg", 
 
      "description":"Three company workers who hate their jobs decide to rebel against their greedy boss.", 
 
      "genre":"Comedy", 
 
      "available":true, 
 
      "rating":"8", 
 
      "releaseYear":"1999" 
 
     }, 
 
     { "title":"Tomorrow Never Dies", 
 
      "image":"images/tnd.jpg", 
 
      "description":"James Bond heads to stop a media mogul's plan to induce war between China and the UK in order to obtain exclusive global media coverage.", 
 
      "genre":"Action", 
 
      "available":true, 
 
      "rating":"7", 
 
      "releaseYear":"1997" 
 
     }, 
 
     { "title":"GoldenEye", 
 
      "image":"images/goldeneye.jpg", 
 
      "description":"James Bond teams up with the lone survivor of a destroyed Russian research center to stop the hijacking of a nuclear space weapon by a fellow agent formerly believed to be dead.", 
 
      "genre":"Action", 
 
      "available":true, 
 
      "rating":"7", 
 
      "releaseYear":"1995" 
 
     }, 
 
     { "title":"Die Another Day", 
 
      "image":"images/dad.jpg", 
 
      "description":"James Bond is sent to investigate the connection between a North Korean terrorist and a diamond mogul who is funding the development of an international space weapon.", 
 
      "genre":"Action", 
 
      "available":false, 
 
      "rating":"6", 
 
      "releaseYear":"2002" 
 
     }, 
 
     { "title":"The World Is Not Enough", 
 
      "image":"images/wne.jpg", 
 
      "description":"James Bond uncovers a nuclear plot when he protects an oil heiress from her former kidnapper, an international terrorist who can't feel pain.", 
 
      "genre":"Action", 
 
      "available":true, 
 
      "rating":"6", 
 
      "releaseYear":"1999" 
 
     }, 
 
     { "title":"Dr. No", 
 
      "image":"images/drno.jpg", 
 
      "description":"A resourceful British government agent seeks answers in a case involving the disappearance of a colleague and the disruption of the American space program.", 
 
      "genre":"Action", 
 
      "available":false, 
 
      "rating":"7", 
 
      "releaseYear":"1962" 
 
     }, 
 
     { "title":"The Rock", 
 
      "image":"images/therock.jpg", 
 
      "description":"A mild-mannered chemist and an ex-con must lead the counterstrike when a rogue group of military men, led by a renegade general, threaten a nerve gas attack from Alcatraz against San Francisco.", 
 
      "genre":"Action", 
 
      "available":false, 
 
      "rating":"7", 
 
      "releaseYear":"1996" 
 
     }, 
 
     { "title":"Dragon Heart", 
 
      "image":"images/dragonheart.jpg", 
 
      "description":"The last dragon and a disillusioned dragonslaying knight must cooperate to stop an evil king who was given partial immortality.", 
 
      "genre":"Action", 
 
      "available":false, 
 
      "rating":"6", 
 
      "releaseYear":"1996" 
 
     }, 
 
     { "title":"Rising Sun", 
 
      "image":"images/risingsun.jpg", 
 
      "description":"At the offices of a Japanese corporation, during a party, a woman, who's evidently a professional mistress, is found dead, apparently after some rough sex. A police detective, Web Smith is called in to investigate but before getting there, he gets a call from someone who instructs him to pick up John Connor, a former police Captain and expert on Japanese affairs. When they arrive there Web thinks that everything is obvious but Connor tells him that there's a lot more going on.", 
 
      "genre":"Action", 
 
      "available":true, 
 
      "rating":"6", 
 
      "releaseYear":"1993" 
 
     }, 
 
     { "title":"Indiana Jones and the Last Crusade", 
 
      "image":"images/indianaLC.jpg", 
 
      "description":"When Dr. Henry Jones Sr. suddenly goes missing while pursuing the Holy Grail, eminent archaeologist Indiana Jones must follow in his father's footsteps and stop the Nazis.", 
 
      "genre":"Action", 
 
      "available":true, 
 
      "rating":"8", 
 
      "releaseYear":"1998" 
 
     }, 
 
     { "title":"Highlander", 
 
      "image":"images/highlander.jpg", 
 
      "description":"An immortal Scottish swordsman must confront the last of his immortal opponent, a murderously brutal barbarian who lusts for the fabled prize.", 
 
      "genre":"Action", 
 
      "available":true, 
 
      "rating":"7", 
 
      "releaseYear":"1986" 
 
     }, 
 
     { "title":"Never Say Never Again", 
 
      "image":"images/neversaynever.jpg", 
 
      "description":"A SPECTRE agent has stolen two American nuclear warheads, and James Bond must find their targets before they are detonated.", 
 
      "genre":"Action", 
 
      "available":true, 
 
      "rating":"6", 
 
      "releaseYear":"1983" 
 
     }, 
 
     { "title":"Diamonds Are Forever", 
 
      "image":"images/diamondsforever.jpg", 
 
      "description":"A diamond smuggling investigation leads James Bond to Las Vegas, where he uncovers an evil plot involving a rich business tycoon.", 
 
      "genre":"Action", 
 
      "available":false, 
 
      "rating":"7", 
 
      "releaseYear":"1971" 
 
     }, 
 
     { "title":"Goldfinger", 
 
      "image":"images/goldfinger.jpg", 
 
      "description":"Investigating a gold magnate's smuggling, James Bond uncovers a plot to contaminate the Fort Knox gold reserve.", 
 
      "genre":"Action", 
 
      "available":true, 
 
      "rating":"8", 
 
      "releaseYear":"1964" 
 
     }, 
 
     { "title":"From Russia with Love", 
 
      "image":"images/russianwithlove.jpg", 
 
      "description":"James Bond willingly falls into an assassination ploy involving a naive Russian beauty in order to retrieve a Soviet encryption device that was stolen by SPECTRE.", 
 
      "genre":"Action", 
 
      "available":true, 
 
      "rating":"8", 
 
      "releaseYear":"1963" 
 
     }, 
 
     { "title":"The Rocky Horror Picture Show", 
 
      "image":"images/rockyhorror.jpg", 
 
      "description":"A newly engaged couple have a breakdown in an isolated area and must pay a call to the bizarre residence of Dr. Frank-N-Furter.", 
 
      "genre":"Comedy", 
 
      "available":false, 
 
      "rating":"8", 
 
      "releaseYear":"1975" 
 
     }, 
 
\t { "title":"Grease", 
 
      "image":"images/grease.jpg", 
 
      "description":"Good girl Sandy and greaser Danny fell in love over the summer. But when they unexpectedly discover they're now in the same high school, will they be able to rekindle their romance?", 
 
      "genre":"Romance", 
 
      "available":true, 
 
      "rating":"7", 
 
      "releaseYear":"1978" 
 
     }, 
 
\t { "title":"Beetlejuice", 
 
      "image":"images/beetlejuice.jpg", 
 
      "description":"A couple of recently deceased ghosts contract the services of a bio-exorcist in order to remove the obnoxious new owners of their house.", 
 
      "genre":"Fantasy", 
 
      "available":false, 
 
      "rating":"7", 
 
      "releaseYear":"1988" 
 
     }, 
 
\t { "title":"Hairspray", 
 
      "image":"images/hairspray.jpg", 
 
      "description":"Pleasantly plump teenager Tracy Turnblad teaches 1962 Baltimore a thing or two about integration after landing a spot on a local TV dance show.", 
 
      "genre":"Romance", 
 
      "available":true, 
 
      "rating":"7", 
 
      "releaseYear":"2007" 
 
     }, 
 
\t { "title":"Little Shop of Horrors", 
 
      "image":"images/HouseofHorrors.jpg", 
 
      "description":"A nerdy florist finds his chance for success and romance with the help of a giant man-eating plant who demands to be fed.", 
 
      "genre":"Fantasy", 
 
      "available":true, 
 
      "rating":"7", 
 
      "releaseYear":"1986" 
 
     }, 
 
\t { "title":"Spaceballs", 
 
      "image":"images/spaceballs.jpg", 
 
      "description":"Planet Spaceballs' President Skroob sends Lord Dark Helmet to steal planet Druidia's abundant supply of air to replenish their own, and only Lone Starr can stop them.", 
 
      "genre":"Sci-Fi", 
 
      "available":false, 
 
      "rating":"7", 
 
      "releaseYear":"1987" 
 
     }, 
 
\t { "title":"The Naked Gun: From the Files of Police Squad!", 
 
      "image":"images/nakedgun.jpg", 
 
      "description":"Incompetent cop Frank Drebin has to foil an attempt to assassinate Queen Elizabeth II.", 
 
      "genre":"Comedy", 
 
      "available":false, 
 
      "rating":"7", 
 
      "releaseYear":"1988" 
 
     }, 
 
\t { "title":"Ghostbusters", 
 
      "image":"images/ghostbusters.jpg", 
 
      "description":"Three former parapsychology professors set up shop as a unique ghost removal service.", 
 
      "genre":"Comedy", 
 
      "available":false, 
 
      "rating":"8", 
 
      "releaseYear":"1984" 
 
     }, 
 
\t { "title":"Gremlins", 
 
      "image":"images/gremlins.jpg", 
 
      "description":"A boy inadvertently breaks 3 important rules concerning his new pet and unleashes a horde of malevolently mischievous monsters on a small town.", 
 
      "genre":"Horror", 
 
      "available":true, 
 
      "rating":"7", 
 
      "releaseYear":"1984" 
 
     }, 
 
\t { "title":"The Hateful Eight", 
 
      "image":"images/hate8.jpg", 
 
      "description":"In the dead of a Wyoming winter, a bounty hunter and his prisoner find shelter in a cabin currently inhabited by a collection of nefarious characters.", 
 
      "genre":"Mystery", 
 
      "available":true, 
 
      "rating":"8", 
 
      "releaseYear":"2015" 
 
     }, 
 
\t { "title":"Inglorious Basterds", 
 
      "image":"images/bastards.jpg", 
 
      "description":"In Nazi-occupied France during World War II, a plan to assassinate Nazi leaders by a group of Jewish U.S. soldiers coincides with a theatre owner's vengeful plans for the same.", 
 
      "genre":"Action", 
 
      "available":false, 
 
      "rating":"8", 
 
      "releaseYear":"2009" 
 
     }, 
 
\t { "title":"Inception", 
 
      "image":"images/Inception.jpg", 
 
      "description":"A thief who steals corporate secrets through use of the dream-sharing technology is given the inverse task of planting an idea into the mind of a CEO.", 
 
      "genre":"Sci-Fi", 
 
      "available":true, 
 
      "rating":"9", 
 
      "releaseYear":"2010" 
 
     }, 
 
\t { "title":"RangiTaranga", 
 
      "image":"images/rangi.jpg", 
 
      "description":"Mysterious events begin to unfold after a reclusive novelist and his wife move back to her ancestral village, followed by a journalist.", 
 
      "genre":"Mystery", 
 
      "available":true, 
 
      "rating":"9", 
 
      "releaseYear":"2015" 
 
     }, 
 
\t { "title":"The Matrix", 
 
      "image":"images/matrix.jpg", 
 
      "description":"A computer hacker learns from mysterious rebels about the true nature of his reality and his role in the war against its controllers.", 
 
      "genre":"Sci-Fi", 
 
      "available":true, 
 
      "rating":"9", 
 
      "releaseYear":"1999" 
 
     }, 
 
\t { "title":"Interstellar", 
 
      "image":"images/interstellar.jpg", 
 
      "description":"A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.", 
 
      "genre":"Sci-Fi", 
 
      "available":false, 
 
      "rating":"9", 
 
      "releaseYear":'2014' 
 
     }, 
 
\t { "title":"Back to the Future", 
 
      "image":"images/bttf.jpg", 
 
      "description":"A young man is accidentally sent thirty years into the past in a time-traveling DeLorean invented by his friend, Dr. Emmett Brown, and must make sure his high-school-age parents unite in order to save his own existence.", 
 
      "genre":"Sci-Fi", 
 
      "available":false, 
 
      "rating":"9", 
 
      "releaseYear":"1985" 
 
     }, 
 
\t { "title":"The Phantom of the Opera at the Royal Albert Hall", 
 
      "image":"images/poto.jpg", 
 
      "description":"A disfigured musical genius, hidden away in the Paris Opera House, terrorizes the opera company for the unwitting benefit of a young protege whom he trains and loves.", 
 
      "genre":"Romance", 
 
      "available":true, 
 
      "rating":"9", 
 
      "releaseYear":"2011" 
 
     }, 
 
\t { "title":"Forrest Gump", 
 
      "image":"images/forrest.jpg", 
 
      "description":"Forrest Gump, while not intelligent, has accidentally been present at many historic moments, but his false love, Jenny Curran, eludes him.", 
 
      "genre":"Romance", 
 
      "available":true, 
 
      "rating":"9", 
 
      "releaseYear":"1994" 
 
     }, 
 
\t { "title":"The Lord of the Rings: The Return of the King", 
 
      "image":"images/lotr.jpg", 
 
      "description":"Gandalf and Aragorn lead the World of Men against Sauron's army to draw his gaze from Frodo and Sam as they approach Mount Doom with the One Ring.", 
 
      "genre":"Fantasy", 
 
      "available":true, 
 
      "rating":"9", 
 
      "releaseYear":"2003" 
 
     }, 
 
\t { "title":"The Green Mile", 
 
      "image":"images/greenmile.jpg", 
 
      "description":"The lives of guards on Death Row are affected by one of their charges: a black man accused of child murder and rape, yet who has a mysterious gift.", 
 
      "genre":"Fantasy, Mystery", 
 
      "available":true, 
 
      "rating":"8", 
 
      "releaseYear":"1999" 
 
     }, 
 
\t { "title":"The Lord of the Rings: The Two Towers", 
 
      "image":"images/lotr2.jpg", 
 
      "description":"While Frodo and Sam edge closer to Mordor with the help of the shifty Gollum, the divided fellowship makes a stand against Sauron's new ally, Saruman, and his hordes of Isengard.", 
 
      "genre":"Fantasy", 
 
      "available":true, 
 
      "rating":"9", 
 
      "releaseYear":"2002" 
 
     }, 
 
\t { "title":"The Lord of the Rings: The Fellowship of the Ring", 
 
      "image":"images/lotr1.jpg", 
 
      "description":"A meek Hobbit and eight companions set out on a journey to destroy the One Ring and the Dark Lord Sauron.", 
 
      "genre":"Fantasy", 
 
      "available":false, 
 
      "rating":"9", 
 
      "releaseYear":"2001" 
 
     }, 
 

 

 

 
    /* DVD data object template 
 
     { "title":"", 
 
      "image":"images/", 
 
      "description":"", 
 
      "genre":"", 
 
      "available":true, 
 
      "rating":"", 
 
      "releaseYear":"" 
 
     }, 
 
    */ 
 
    ];

我是新使用cookies与角JS来存储和检索的cookie。我需要确保我的应用程序的基本功能设置为使用cookie。还有更多的代码,但这是我处理我的cookies的。我是否在模块和控制器中包含了适当的东西,并且我是否正确地包含了js文件。每当我尝试打印cookie的值时,我将其检索到控制台,我得到未定义。此外,我的购物车变量是和我知道我需要将其转换为字符串的任何方向的对象数组将非常感谢以及。

var dvdApp = angular.module("dvdApp", ["ngCookies"]); 
dvdApp.controller("dvdController", dvdController); 

    function dvdController($scope, $cookies) { 

     $scope.save_data = function(){ 
      var n = $scope.number_items_cart.toString(); 
      $cookies.put("shoppingCart", $scope.shopping_cart); 
      $cookies.put("cartCount", n); 
     } 

     $scope.get_cookie = function(){ 
      var cartCookie = $cookies.get("shoppingCart"); 
      var countCookie = $cookies.get("cartCount"); 
      console.log(cartCookie); 
      console.log(countCookie); 
     } 
<script src="http:////ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-cookies.js"></script> 

回答

1

你处理Cookie的方式似乎是正确的。就购物车而言,你可以简单地使用JSON.stringify它。

要保存购物车:

$cookies.put("shoppingCart", JSON.stringify($scope.shopping_cart)); 

要检索购物车:

var cartCookie = $cookies.get("shoppingCart"); 
if (cartCookie) { 
    cartCookie = JSON.parse(cartCookie); 
} 
+0

我保持我每次调用get_cookie功能时越来越不确定。 –

+0

它说什么是未定义的?你有控制台错误? – TheBanHammer

+0

没有错误。告诉功能打印到控制台的部分只打印未定义的部分。即使我以字符串的形式将值硬编码到密钥。 –