如何清除AngularJS中的输入字段

时间:2014-05-25 05:56:22

标签: javascript angularjs

我试图通过ngClick提交后清除html页面上的输入文本字段。

<html ng-app="myApp">
<head lang="en">
<title>Test</title>
<script>
 angular.module("myApp", [])
            .controller("defaultCtrl", function ($scope) {
                $scope.members = [
                    {name: "Joe", age: 24},
                    {name: "Jane", age: 22}
                ];

                $scope.newMember = {};

                $scope.addMember = function (member) {
                    $scope.members.push(member);
                }
            });
</script>
</head>
<body ng-controller="defaultCtrl">
<table class="table table-bordered">
<thead>
    <td>Name</td>
    <td>Age</td>
    <td></td>
</thead>
<tbody>
<tr  ng-repeat="member in members">
    <td>{{member.name}}</td>
    <td>{{member.age}}</td>
</tr>
<tr>
    <td>
        <input type="text" ng-model="newMember.name" />
    </td>
    <td>
        <input type="text" ng-model="newMember.age" />
    </td>
    <td>
        <button class="btn btn-xs" ng-click="addMember(newMember)">Add</button>
    </td>
</tr>
</tbody>
</table>
</body>
</html>

这是jsFiddle中的a link to the sample

我不希望新名称/年龄字段显示为预先填充了我刚刚单击“添加”后添加的名称/年龄输入。

感谢。

1 个答案:

答案 0 :(得分:0)

只需重新初始化newMember对象即可正常工作

   $scope.addMember = function (member) {
       $scope.members.push(member);
       $scope.newMember = {};
   }

请参阅小提琴http://jsfiddle.net/cmyworld/G6M8q/