我正在使用AngularJS,并且我有一个表单,用户可以在其中输入数据。在表单的末尾,我想有两个按钮,一个按钮是“保存”,将保存并转到另一页,另一个按钮是“保存并添加另一个”,将保存该表单,然后将其重置-允许他们输入另一个条目。

我该如何做到这一点?我当时以为我可以有两个带有ng-click标签的提交按钮,但是我在form元素上使用ng-submit。是否有任何我需要使用ng-submit的原因-我不记得为什么我开始使用它而不是 ng-click 按钮。

该代码如下所示:

<div ng-controller="SomeController">
    <form ng-submit="save(record)">
        <input type="text" name="shoppingListItem" ng-model="record.shoppingListItem">
        <button type="submit">Save</button>
        <button type="submit">Save and Add Another</button>
    </form>
</div>

并在 Controller 中SomeController
$scope.record = {};
$scope.save = function (record) {
    $http.post('/api/save', record).
        success(function(data) {
            // take action based off which submit button pressed
        });
}

最佳答案

ngSubmit允许您在文本框上键入要提交时按Enter。如果该行为并不重要,请使用2 ngClick。如果重要的话,您可以修改第二个按钮以使用ngClick。因此,您的代码变为:

<div ng-controller="SomeController">
    <form ng-submit="save(record)">
        <input type="text" name="shoppingListItem" ng-model="record.shoppingListItem">
        <button type="submit">Save</button>
        <button ng-click="saveAndAdd(record)">Save and Add Another</button>
    </form>
</div>

10-04 21:24
查看更多