AngularJS: ng-model不清除输入

AngularJS: ng-model not clearing input

本文关键字:清除 输入 ng-model AngularJS      更新时间:2023-09-26

我有一个动态的cpf(巴西"社会安全"号码)输入。

每次我输入一个CPF,应该显示另一个输入,以此类推。但由于某种原因,添加CPF后ng模型没有被清除。

这是我的HTML(在一个具有隔离作用域的指令中):

<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>

这是我的控制器(与隔离范围内的指令):

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

$scope.cpf.number = "";代替delete $scope.cpf;

我们不能删除一个模型,我们必须将其设置为空白,因为它与我们的视图部分链接在一起