S
S
Skrolea2016-01-24 18:49:28
JavaScript
Skrolea, 2016-01-24 18:49:28

How does Javascript work?

There is a basket script (written in angular, but that's not the point).
I can not understand.

vm.cartItems = [];
        function addItemsList(id, name, type) {
            var objItems = {
                id: id,
                name: name,
                type: type
            };  
                vm.cartItems.push(objItems);
              }

I add items to the cart. I withdraw from the service
function getItemsList() {
                     console.log('корзина' +vm.cartItems);
           
           return vm.cartItems;
        }

So. Everything is displayed on the screen (well, there is a directive). But! The output console.log('корзина' +vm.cartItems); shows nothing. Only "basket".
Why is that?
Added Plunker

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton Mudrenok, 2016-01-24
@Skrolea

try passing this vm.cartItems to the function, perhaps at the time of the function execution this object is read from the scope, where it is empty
i.e. at the time of output to the console, you are transferred (more precisely, it is not transferred, but read) an empty array, but then it is filled and you see that everything is working fine

A
Alexander, 2016-01-24
@aleksand44

I may be wrong, but here, in my opinion, it is necessary to use the enumeration of the object's properties using for in
. Neither the alert nor the console.log shows the contents of the object by simply calling the variable that contains it

A
Andrey Dyrkov, 2016-01-24
@VIKINGVyksa

try
console.log('Корзина',vm.cartItems);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question