1
var product_id = $this.attr('data-id');
angular.forEach($rootScope.cartItems, function(value, key) {
if (value.iProductID == product_id && key > -1){
    $rootScope.cartItems.splice(key, 1);
    console.log($rootScope.cartItems);
}

Here in above code, I'm removing object element from the array but after removing element using splice the console.log() displays the correct data results. But in my HTML $rootScope.cartItems still displays the removed element.

1
  • Can you please add html of your view and how your cartItems are used in it? Are there some ng-if around? Why do you need to use $rootScope btw? Most of all due to scope's prototypal inheritance you can see some other array in your view but not the one you are changing in your $rootScope. Commented Jun 22, 2017 at 7:50

2 Answers 2

1
$rootScope.cartItems.splice(key, 1);
                    $scope.$apply(function() {
                        $rootScope.cartItems = $rootScope.cartItems;
                    });

I found the answer. Thank you guys for your answers.

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

Comments

0

Probably some block of code in controller refills the cart items and hence the change is not visible.

You could set up an event to listen to remove an item from the cart.

var product_id = $this.attr('data-id');
$rootScope.$broadcast('RemoveCartItem', product_id);

Listen for changed event as:

$rootScope.$on('RemoveCartItem', function (event, product_Id) {
    angular.forEach($rootScope.cartItems, function(value, key) {
    if (value.iProductID == product_Id && key > -1){
        $rootScope.cartItems.splice(key, 1);
        console.log($rootScope.cartItems);
    }
});

Comments

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.