【问题标题】:Submit and going back on same button click提交并返回相同的按钮单击
【发布时间】:2014-07-03 05:37:57
【问题描述】:

我有一个表单,它有一个取消按钮和一个保存按钮。如果你点击取消,它应该返回上一页,如下所示:

<a href="#/study">
<button type="button" class="btn btn-default"
        data-dismiss="modal" ng-click="clear()">
        <span class="glyphicon glyphicon-ban-circle"></span> Cancel
</button>
</a>

我正在使用简单的锚标记来实现这一点。现在,当表单填写完毕并点击保存按钮时,它应该调用控制器上定义的 create() 方法从表单输入创建我想要的对象并返回上一页

<a href="#/study">
<button type="submit" ng-click="go('#/study')" ng-       
        disabled="form.$invalid" class="btn btn-primary">
        <span class="glyphicon glyphicon-save"></span> Save
</button>
</a>

当然它会调用控制器上的create方法并保存表单输入但不会返回上一页。我将非常感谢任何正确方向的指示。

【问题讨论】:

  • 如果您在控制器中提交表单,只需将 go('#/study') 调用放入控制器方法中即可提交。

标签: javascript angularjs href


【解决方案1】:

您可以使用$location 服务及其path 方法重定向到特定路径或使用$window.history.back() 移动到最后一页。

app.controller('ctrl',function($scope, $location, $window){

    $scope.submit = function(){
        $http.post(...).success(function(){
            $location.path('/study');           
            //or            
            $window.history.back();
        });
    };

});

按钮:

<button type="submit" ng-click="submit()" ng-       
        disabled="form.$invalid" class="btn btn-primary">
        <span class="glyphicon glyphicon-save"></span> Save
</button>

【讨论】:

    猜你喜欢
    • 2013-08-20
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    相关资源
    最近更新 更多