Reputation: 8922
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();
}
Upvotes: 1
Views: 5414
Reputation: 4678
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();
}
Upvotes: 0
Reputation: 25882
You might be trying to push in $scope
instead of $scope.array
. Error is like that.
Upvotes: 0
Reputation: 491
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
Upvotes: 1