K
K
Konstantin2015-04-13 00:11:05
JavaScript
Konstantin, 2015-04-13 00:11:05

Why doesn't Angular JS/Javascript add an object to the array?

If you display the current object $scope.messagesin the console:

Object {1: Object, 2: Object, 3: Object, 4: Object}

If you view the first object:
1: Object  { dialog: "534_544"from: "534"message: "Test mEssage"read: "0"time: "1428852706"to: "544" }

I'm trying to add a new element to the $scope.messages array:
var i = {
         dialog: "534_544",
         from: "538",
         message: "Test dmEssage",
         read: "0",
         time: "528852706",
         to: "546"
};
                $scope.messages.push(i);

But it doesn't work - throws Angular JS errors.
If you do this, then it works:
$scope.messages[500] = {
                    dialog: "534_544",
                    from: "538",
                    message: "sex sex",
                    read: "0",
                    time: "528852706",
                    to: "546"
                };

I don't understand what is wrong?
I did a test for an example - I found the difference:
https://jsfiddle.net/q8fg4rzv/4/

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lega, 2015-04-13
@Junart1

Because you have messages - an object, not an array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question