如何使用按钮清除具有相同ng-model输入的表单?

时间:2015-03-09 12:59:19

标签: angularjs angularjs-ng-repeat

我有一个表单,它根据我之前选择的内容输入输入字段,因此我的表单中可以有不同数量的输入。我试过这样的事情:

$scope.person = {
      firstName: "John",
      lastName: "Doe"
    };

    var oriPerson = angular.copy($scope.person);
$scope.resetForm = function ()
    {
      $scope.person = angular.copy(oriPerson);
      $scope.personForm.$setPristine();
    };

但问题是我可以输入相同的ng-model,所以例如当我尝试写" blabla"在输入中使用ng-model ="整数",它会自动输入" blabla"在其他输入中使用ng-model ="整数"。帮助

由于

1 个答案:

答案 0 :(得分:0)

您将多个控件绑定到同一属性,ng-model将控件绑定到属性,在您的情况下属性为“整数”。如果您希望它采取不同的行为,您将需要不同的属性。即interger,coolInteger,dumbInteger,awesomeInteger等等。

如果要根据选择添加输入,则需要具有将ng-model命名为不同的逻辑,以便它们不会绑定到相同的值。如果值与人物相关联,则可以将名称基于范围内的人员,例如:

ng-model="person.firstName"

然后,如果你有多个人,你可以在你想要的地方复制你想要的东西。

$scope.resetForm = function ()
{
  $scope.person2 =  $scope.person;
  $scope.person = null;
  // whatever else....
};

然后清除person.firstName的绑定输入,并获得副本。希望有所帮助。