Error: push is not a Function

Asked

Viewed 2,470 times

0

As I click the button it should add what is written in the fields in the table, but it of the error:

Typeerror: $Scope.listClient.push is not a Function

My controller:

app.controller('MainController', ['$scope', function($scope) {
$scope.client = {};
$scope.listClient = {};

$scope.addClient = function(client){
    $scope.listClient.push(client);
    $scope.client = {};
}

$scope.deleteClient = function(indice){
    $scope.listClient.splice(indice,1);
};

}]);

Index:

<!DOCTYPE html>
<html lang="pt-br" xml:lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <title>Cadastro de Pessoas</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular.min.js" ></script>
    <script src="js/app.js" ></script>
    <script src="js/controllers/Client.js" ></script>
</head>
<body ng-app="myApp">
    <div class="content" ng-controller="MainController">
        <form ng-submit="addClient(client)">
            <div class="dadosPessoais">
                <label>Nome: </label>
                <input type="text" ng-model="client.nome">
                <label>Sobrenome: </label>
                <input type="text" ng-model="client.sobrenome">
                <label>Email: </label>
                <input type="text" ng-model="client.email">
                <label>Telefone: </label>
                <input type="text" ng-model="client.telefone">
            </div>
            <input type="submit" value="Adicionar"> 
        </form>
        <table border="1">
            <tr>
                <th>Nome</th>
                <th>Sobrenome</th>
                <th>Telefone</th>
                <th>E-mail</th>
                <th>Excluir</th>
            </tr>
            <tr ng-repeat="client in listClient track by $index">
                <td>{{client.nome}}</td>
                <td>{{client.sobrenome}}</td>
                <td>{{client.telefone}}</td>
                <td>{{client.email}}</td>
                <td ng-click="deleteClient($index)">X</td>
            </tr>
        </table>
    </div>
</body>
</html>

2 answers

0


You are using the variable listClient as an object and not as a array. Objects do not have the function push. Change the variable initialization to:

$scope.listClient = [];

0

Because there is no function push in an object, what you probably want is to add an item to the array, so you need to define listClient as an array:

$scope.listClient = [];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.