user3522457
user3522457

Reputation: 2963

cannot read property 'push' of undefined

$scope.AddTask = function () {
    $scope.tasks.push([{
        "taskName": $scope.taskName,
        "priorty": $scope.selectedP
    }]);
};
$scope.tasks = [{
    "taskId": 1,
    "taskName": "task 1",
    "priorty": 1
}, {
    "taskId": 2,
    "taskName": "task 2",
    "priorty": 2
}, {
    "taskId": 3,
    "taskName": "task 3",
    "priorty": 3
}];

I got an error of cannot read property 'push' of undefined in angularjs

app demo : http://plnkr.co/edit/ObKoQn2tZ4evgJpKQBpH?p=preview

Upvotes: 2

Views: 26092

Answers (2)

Victor Queiroz
Victor Queiroz

Reputation: 123

See this function:

$scope.AddTask = function() {
  $scope.tasks.push([{
    "taskName": $scope.taskName,
    "priorty": $scope.selectedP
  }]);
};

You must remove [] before {} in $scope.tasks.push, they're pushing another array inside $scope.tasks and they must push an object.

Upvotes: 1

Simon Wicki
Simon Wicki

Reputation: 4059

your plnkr looks fine. although you should use

$scope.tasks.push({
    "taskName" : $scope.taskName,
    "priorty": $scope.selectedP
});

instead of an additional [].

Upvotes: 3

Related Questions