1

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;
 };
1
  • use $scope.cpf = undefined; Commented Oct 7, 2015 at 5:46

1 Answer 1

3

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

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

2 Comments

please add a brief description as to how your solution resolves the askers problem. How to Answer
we can't delete an model, we have to set it to blank, because its linked with our View part

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.