2017-03-05 26 views
0

任何人都可以看看这段代码,并解释为什么我无法在我的购物车中显示物品。它是一个简单的购物车,我显示阵列产品的列表,当我点击每个项目的详细信息时显示添加到购物车选项,但是当我点击我的购物车中的项目它不显示项目添加?我无法显示我的购物车angularjs中的项目?

这里是我的链接plunker:https://plnkr.co/edit/oo05d6H6AxuJGXBAUQvr?p=preview

这是我的script.js

// 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(); 



    } 
]); 

你需要把$scope在HTML中使用它!

+0

谢谢!它的工作,我也可以做一些事情,当用户点击添加到购物车按钮类似添加成功应该弹出? – vennapusa

+0

请您接受答案。所以可以认为是封闭的。点击右下方的右侧刻度。 – 2017-03-05 06:07:43

+0

是的,你也可以这样做。 U可以在'add product'下面'alert(item.name +“成功添加)''' – 2017-03-05 06:09:01

相关问题