Search code examples
javascriptangularjsdirectiveangular-ngmodel

AngularJS: ng-model not clearing input


I have a dynamic input for CPFs (Brazilian 'social security' number).

Every time I enter one CPF, another input should be displayed, and so on.. But for some reason, the ng-model is not being cleared after the CPF is added.

Here's my HTML (inside a directive with isolate scope):

<div ng-repeat="cpf in cpfs" class="field-input">
    <input type="text" class="field" ng-model="cpf.number" required>
    <label>added CPF</label>
</div>
<div class="field-input">
     <input type="text" class="field" ng-model="cpf.number" required>
     <label>add new CPF</label>
     <button ng-click="addCpf(cpf)" class="btn btn-primary">add</button>
</div>

Here's my controller (inside a directive with isolate scope):

$scope.cpfs = [];

$scope.addCpf = function(cpf) {
     $scope.cpfs.push(angular.copy(cpf));     
     delete $scope.cpf;
 };

Solution

  • instead of delete $scope.cpf; use $scope.cpf.number = "";

    we can't delete an model, we have to set it to blank, because its linked with our View part