停止“?"从添加到URL [英] Stop "?" from being added to URL

查看:65
本文介绍了停止“?"从添加到URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个Angular 2应用程序,用户可以在其中提交表单以创建新项目.单击提交按钮后,它将调用一个函数,该函数将数据发送到服务器,并在服务器确认数据已成功保存后导航到新页面.

I currently have an Angular 2 app where the user can submit a form to create a new item. When the submit button is clicked, it calls a function that sends the data to the server and navigates to a new page when the server confirms that the data has been saved successfully.

我的问题来了,因为表单提交将表单参数附加到URL.因此,例如,如果我有一个名为title的输入,并且提交将我带到路径mytitle(这是title字段的输入),则Angular(或注入GET参数的任何内容)将尝试导航至mysite.com/mytitle?title=mytitle而不只是mysite.com/mytitle.即使将[ngModelOptions]="{standalone: true}"添加到我的所有输入中,仍然在其后留下一个不带任何参数的问号.

My problem comes because the form submission appends the form parameters to the URL. So for example if I had an input named title and submission took me to the route mytitle which is the input for the title field, Angular (or whatever injects the GET parameters) would try to navigate to mysite.com/mytitle?title=mytitle instead of just mysite.com/mytitle. Even adding [ngModelOptions]="{standalone: true}" to all of my inputs still leaves a question mark with no parameters after it.

这是一个问题,因为它导致Angular重新加载应用程序,因为给定的路由与我的路由定义中的任何路由都不匹配.有没有办法完全禁止将GET参数注入URL? POST也不起作用,因为我无处可发布,并且我的下一个URL使用了表单本身的数据.

This is a problem because it causes Angular to reload the app because the given route does not match any routes in my route definitions. Is there a way to disable the GET parameters being injected into the URL entirely? POST doesn't work either because I have nowhere to post to, and my next URL uses data from the form itself.

推荐答案

我找到了我的问题的答案,提交"按钮默认为提交"类型,因此将其更改为按钮"类型删除了GET参数注入行为.

I found an answer to my question, the "Submit" button defaulted to being of type "submit", so changing it to type "button" removed the GET parameters injection behavior.

这篇关于停止“?"从添加到URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆