Javascript “从购物车中删除”在AngularJS中不起作用

Javascript “从购物车中删除”在AngularJS中不起作用,javascript,html,angularjs,ionic-framework,Javascript,Html,Angularjs,Ionic Framework,这是我的密码 $scope.cart = []; $scope.addToCart = function (cat) { var found = false; $scope.cart.forEach(function (item) { if (item.id === cat.product_id) { item.quantity++; found =

这是我的密码

     $scope.cart = [];

        $scope.addToCart = function (cat) {
        var found = false;
        $scope.cart.forEach(function (item) {
            if (item.id === cat.product_id) {
                item.quantity++;
                found = true;
            }
        });
        if (!found) {
            $scope.cart.push(angular.extend({quantity: 1}, cat));

        }
    };

//remove from cart function 
        $scope.removeToCart = function (cat) {
            console.log(cat.product_id);
            var found = false;
            $scope.cart.forEach(function (item) {
                if (item.id === cat.product_id) {
                    item.quantity--;
                    found = true;
                }
            });
            if (!found) {
                $scope.cart.push(angular.extend({quantity: 1}, cat));



        }
    };

    console.log($scope.cart);

        $scope.getCartPrice = function () {
            var total = 0;

            $scope.cart.forEach(function (cat) {
                total += cat.finalprice * cat.quantity;
            });
            return total;
        };

可能需要从removeToCart函数中删除此位:

if (!found) {
   $scope.cart.push(angular.extend({quantity: 1}, cat));
}

如果在购物车中找不到“猫”,您将始终添加数量为1的猫。对我来说,“addToCart”和“removeToCart”似乎都是一样的,只是item.quantity++和item.quantity-行不同

您可能需要从removeToCart函数中删除此位:

if (!found) {
   $scope.cart.push(angular.extend({quantity: 1}, cat));
}

如果在购物车中找不到“猫”,您将始终添加数量为1的猫。对我来说,“addToCart”和“removeToCart”似乎都是一样的,只是item.quantity++和item.quantity-行不同

你能更详细地描述一下你在《猫》中传递的信息吗?如果它是一个对象,你能分享它的详细信息吗?问题现在看起来有点不清楚。您还可以演示如何调用remove函数吗?我在cat中传递product_id,不,这不是对象0您可以更详细地说明在“cat”中传递的内容吗?如果它是一个对象,你能分享它的详细信息吗?问题现在看起来有点不清楚。您还可以演示如何调用remove函数吗?我正在cat中传递product_id,不,这不是对象0