2017-03-05 10 views
0

誰でもこのコードを調べて、カートにアイテムを表示できない理由を説明できます。その単純なショッピングカートでは、配列から商品のリストを表示し、各アイテムの詳細をクリックするとカートオプションに追加されますが、カート内のアイテムをクリックするとアイテムが表示されません。ここ私のカートanglejsにアイテムを表示できませんか?

はplunkerに私のリンクです:https://plnkr.co/edit/oo05d6H6AxuJGXBAUQvr?p=preview

これは私のscript.jsあなたがHTMLでそれを使用する$scopeを配置する必要があり

// Code goes here 



var app = angular.module("myApp", ["ngRoute"]); 
app.controller('mobileController', function($scope) { 
    $scope.items = [{ 
    name: 'Iphone', 
    price: 70000, 
    rating: '*****', 
    image: 'http://i.imgur.com/hfMaGTN.png' 
    }, { 
    name: 'Oneplus', 
    price: 60000, 
    rating: '****', 
    image: 'http://i.imgur.com/sBcs5ZE.jpg' 
    }, { 
    name: 'Samsung', 
    price: 50000, 
    rating: '***', 
    image: 'http://i.imgur.com/8Bexxbf.jpg' 
    }, { 
    name: 'Sony', 
    price: 40000, 
    rating: '***', 
    image: 'http://i.imgur.com/0c7PcX8.png' 
    }, { 
    name: 'Moto', 
    price: 20000, 
    rating: '****', 
    image: 'http://i.imgur.com/HyWR1VE.png' 
    }]; 
}); 

app.service("cartService", [function(){ 

    var cart = []; 

    function getCart(){ 
    console.log(cart); 
    return cart; 
    } 

    function addToCart(item){ 
    cart.push(item); 
    console.log(cart); 
    } 

    return { 
    getCart: getCart, 
    addToCart: addToCart 
    }; 

}]); 

app.config(function($routeProvider) { 
    $routeProvider 
    .when("/store", { 
templateUrl : "store.html", 

}) 

.when('/item/:itemName', { 
     templateUrl: 'details.html', 
     controller: 'ItemCtrl' 
    }) 
    .when("/cart", { 
     templateUrl: 'cartdetails.html', 
     controller: 'cartCtrl' 
    }); 
}); 

app.controller('ItemCtrl', ['$scope', '$routeParams', 'cartService', 
    function($scope, $routeParams, cartService) { 
    $scope.item = {}; 
    angular.forEach($scope.items, function(item) { 
     if (item.name == $routeParams.itemName) { 
     $scope.item.itemName = item.name; 
     $scope.item.itemPrice = item.price; 
     $scope.item.itemRating = item.rating; 
     $scope.item.itemImage = item.image; 
     } 
    }); 

    $scope.addProduct = function(item){ 
     console.log(item); 
     cartService.addToCart(item); 
    }; 
    } 
]); 
app.controller('cartCtrl', ['$scope','cartService', 
    function($scope,cartService) { 
    $scope.name="sasiree"; 

    var cartItems=cartService.getCart(); 



    } 
]); 
+1

これはバインドしていないためです。 $ scope.cartItems = cartService.getCart();代わりにvar cartItems = cartService.getCart(); – Fetrarij

答えて

1
app.controller('cartCtrl', ['$scope','cartService', 
    function($scope,cartService) { 
    $scope.name="sasiree"; 

    $scope.cartItems=cartService.getCart(); 



    } 
]); 

です!

+0

ありがとう!それはうまくいきました。ユーザーがカートボタンに追加ボタンをクリックしたときに、正常に追加されたようなものがポップアップするような何かをすることができますか? – vennapusa

+0

答えを受け入れることはできますか?したがって、クローズとみなすことができます。ダウンローダーの下の右のチェックマークをクリックしてください。 –

+0

はい、あなたもそれを行うことができます。 Uは 'add product'の下で(item.name +" added successfully ")'警告することができます –

関連する問題