简体   繁体   English

AngularJS表单提交和重定向

[英]AngularJS Form Submit and Redirect

Is there a way to submit a form with AngularJs without using an asynchronous call? 有没有一种方法可以使用AngularJs提交表单而不使用异步调用? I'd like it to act as if I hit the submit button on a regular HTML only form. 我希望它的行为就像我在常规的仅HTML表单上单击“提交”按钮一样。

HTML: HTML:

<div ng-controller="myController">
    <form id="myForm">
      <input ng-model="handle">
      <button ng-click="submitForm($event)">Submit</button>
    </form>
</div>

Currently I'm doing it like I am below. 目前,我正在做下面的事情。 The problem is, I'm still on the same page, and then I have to redirect. 问题是,我仍然在同一页面上,然后必须重定向。 But the next page's content depends on the form submission, and I'd rather not store the contents of the form submission in the session. 但是下一页的内容取决于表单提交,因此我宁愿不在会话中存储表单提交的内容。

angular.module('myApp').controller('myController', ['$scope', function($scope) {

    $scope.handle;

    $scope.submitForm = function($event) {

        $event.preventdefault();

        var modifiedHandle= $scope.handle + 'handle';

        $http({
            url: '/submit',
            method: 'POST',
            data: {'handle': modifiedHandle }
        }).then(function(response){

           $window.location.href = '/success';

           //The problem of this approach is that the contents of '/success' depends on the form input.
           //I'd rather not have to flash the submitted data to a session. Is there a pure AngularJs way to do this?

        }, function(data, status){});

    }


}]);

If you are using ui-router instead of ngRoute then you can pass an object to a new state as a parameter. 如果使用ui-router而不是ngRoute,则可以将对象作为参数传递给新状态。 This parameter does not end up as a query string in the URL. 该参数不会以URL中的查询字符串结尾。 This approach is detailed in this answer and I have used this approach many times. 答案中详细介绍了这种方法,我已经使用了很多次。 So instead of using $window you would use $state.go. 因此,不是使用$ window,而是使用$ state.go。

Alternatively, you have 2 other options. 另外,您还有2个其他选择。 You could put your object directly on the $rootScope, making it accessible from the controller of your success page. 您可以将对象直接放在$ rootScope上,从而可以从成功页面的控制器进行访问。 Generally speaking you do not want to pollute you're rootScope, but it will work. 一般来说,您不想污染您的rootScope,但它会起作用。

The most pragmatic angular approach I think would be to create a service that stores the value. 我认为最实用的角度方法是创建一个存储值的服务。 Services are singletons and as such when you set a value, it will be accessible from other controllers. 服务是单例,因此,当您设置值时,可以从其他控制器访问它。 Your service may look something like this: 您的服务可能看起来像这样:

app.service("Store", function() {

    var formObject = {};

    this.set = function(object) {
        formObject = object;
    };

    this.get = function() {
        return formObject;
    };

    return this;
});

Then after your successful form submission you would call Store.set(yourObject), and then on load of your new controller you could call Store.get() to return the value. 然后,在成功提交表单后,您将调用Store.set(yourObject),然后在加载新控制器时可以调用Store.get()返回该值。 Read more about angular services from the docs . 文档中了解有关角度服务的更多信息。

Sure this may be considered a session, but it is the angular way if the other two options provided don't suite your needs. 当然,可以将其视为会话,但是如果提供的其他两个选项都不适合您的需求,则这是一种有角度的方法。

Using Agular Js CLI you can redirect your page on successful submition form by: 使用Agular Js CLI,您可以通过以下方式重定向成功提交表单上的页面:

postData(email){
    if(email==""){
      alert('email feild is empty');
      return false;
    }
    else{
      window.location.href = 'https:wwww.google.com';
    }
  }
  • here email under postData(email) is basically a model that is using in html code 这里POSTDATA(电子邮件)在电子邮件基本上是在HTML代码中使用的模型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM