【问题标题】:Refreshing and redirecting page after clicking button单击按钮后刷新和重定向页面
【发布时间】:2016-07-12 11:32:27
【问题描述】:

问题:

单击“立即发布”后仍会显示已填写表格的模式,如果再次单击“立即发布”,它会一次又一次地添加相同的数据。

我想在填写完所有表单并单击按钮后,刷新页面并转到带有预测描述的链接。

Prediction.createPrediction($scope.prediction).then(function(ref) {
            $toaster.pop('success', 'Prediction posted successfully.');
            $scope.prediction = {
                player1: '',
                player2: '',
                winner: '',
                venue: '',
                date: '',
                round: '',
                description: '',
                odd: '',
                score: '',
                status: 'open',
                gravatar: '',
                name: '',
                poster: ''
            };
            $location.path('/browse/' + ref.key());
        });

应用链接:https://sportpredictions.firebaseapp.com/#/ (发布预测仅适用于登录用户)

GitHub 上的源代码:https://github.com/NiR-IT/tennispredictions(scripts/services/prediction.js、scripts/controllers/prediction.js、views/partials/post.html)。

【问题讨论】:

  • 我们是否假设这不起作用?发生了什么?
  • 在模态中单击按钮提交后页面不刷新。因此,如果手动关闭模态并再次单击“发布预测”按钮,则会出现相同的模态,已填写表单。

标签: javascript jquery angularjs firebase


【解决方案1】:

试试这个:在表单中添加一个name="postForm" 属性。这将使它可以通过 $scope.postForm 在您的控制器中访问,然后您可以调用 $scope.postForm.$setPristine() 这应该重置表单上的所有内容 - 包括任何验证错误等。

附:真的很漂亮的应用程序!

【讨论】:

  • 我应该把 $scope.postForm.$setPristine() 放在哪里?并且在控制台中也出现错误“$toaster is not defined”(当我点击提交按钮时”。谢谢,Lex :)
  • 您将toaster 注入您的控制器,因此您需要在没有$ 的情况下调用它。我会在您的烤面包机消息之后立即拨打$setPristine() 的电话。您可能可以摆脱该代码的其余部分。
  • 所以事情现在发生了同样的情况,如果把 jquery 代码 $('#Button').click(function() { location.reload(); });单击按钮后页面重新加载,但数据库也没有使用新的预测更新;(
  • 等等,你为什么要使用 jQuery 重新加载页面?对不起,我现在完全糊涂了。我以为您正在使用 Angular 进行重定向。如果可能的话,我强烈建议不要混合使用 jQuery 和 Angular。
  • 哦,在这种情况下我没有使用 jQuery,抱歉。但我试过了,同样的事情发生了。所以点击按钮页面重新加载后,是的,但是数据(预测)没有添加到数据库中。 :(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-21
  • 2014-04-10
  • 1970-01-01
  • 2013-10-13
  • 1970-01-01
  • 2019-08-22
  • 1970-01-01
相关资源
最近更新 更多