1

When I need to push a new item in an empty array, I got an error : $scope.array.push is not a function.

But when the array is not empty, the push works perfectly and it correctly updates my scope.

For information, my array is instanciated like this :

$scope.array = [];

I tried too :

$scope.array = [{}]

How can I bypass this problem ? Or what am I doing wrong ?

EDIT :

$scope.urgences_hygienes = [];

$scope.save = function () {
    $scope.newDemandeHygiene = {
        id_zone : $scope.demandeHygiene.id_zone,
        commentaire : $scope.demandeHygiene.commentaire,
        date_demande : Math.round(new Date().getTime() / 1000)
    }
    $scope.urgences_hygienes.push( $scope.newDemandeHygiene );
    $scope.reloadTable();
    $modalInstance.close();
}
1
  • show the line how are you pushing? Commented Jul 10, 2014 at 7:09

5 Answers 5

1

the right syntax is: $scope.myarray.push(someobject)

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

Comments

1

It looks like you're using $scope.push(var) instead of $scope.array.push(var).

Additionally, it's worth note that it's faster to just use $scope.array[$scope.array.length]=var

2 Comments

I ve editted my post, my bad to have forgotten to write $scope.array.push(var) in the error log.
That second statement is a bit of a sweeping generalisation, it varies significantly cross-browser and often dependent on the array length itself. In any case it's a micro-optimisation that's not worth asserting if someone hasn't specifically asked about it. Readability should be the important factor in that decision.
0

You might be trying to push in $scope instead of $scope.array. Error is like that.

Comments

0

Try

$scope.array.push({});

instead.

1 Comment

Have a look at this. jsfiddle.net/Azamanaza/TRy6X/5 Could you post more details on this code? The problem might be else where...
0

The overall code must be this way.

$scope.save = function () {
    $scope.urgences_hygienes = [];

    $scope.newDemandeHygiene = {
        id_zone : $scope.demandeHygiene.id_zone,
        commentaire : $scope.demandeHygiene.commentaire,
        date_demande : Math.round(new Date().getTime() / 1000)
    }
    $scope.urgences_hygienes.push( $scope.newDemandeHygiene );
    $scope.reloadTable();
    $modalInstance.close();
}

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.