简体   繁体   English

使用表单提交中的URL发送routeparams

[英]Sending routeparams with the URL on form submit

I need to send route params with the URL when the user clicks on submit button. 当用户单击“提交”按钮时,我需要使用URL发送路由参数。

Here is my simple form 这是我的简单表格

<form action="#/chat/{{username}}">
    <input type="text" placeholder="enter a username..." ng-model="username" required  autofocus><br/>
    <input type="submit" value="submit">
  </form>

But this doesn't work as the 'username' variable doesn't bind and it goes to /chat/%7B%7Busername%7D%7D instead (somebody enlighten me on why this happens) 但这不起作用,因为'username'变量未绑定,而是转到/chat/%7B%7Busername%7D%7D (有人启发了我为什么会发生这种情况)

Currently, the workaround I am following is using a hyperlink instead of a submit button 当前,我要解决的解决方法是使用超链接而不是提交按钮

 <div>
    <input type="text"  class="form-control" placeholder="enter a username..." ng-model="username" required  autofocus><br/>
    <a href="#/chat/{{username}}" class="btn btn-lg btn-primary btn-block"  >Start Chatting</a>
  </div>

But the problem with the above approach is that it doesn't work when the user presses ENTER key (as it is not the submit button) 但是上述方法的问题在于,当用户按下ENTER键时,它不起作用(因为它不是“提交”按钮)

So what is the correct way to achieve this? 那么实现这一目标的正确方法是什么?

Markup: 标记:

<form ng-submit="onSubmit()">
  <input type="text" placeholder="enter a username..." ng-model="username" required  autofocus><br/>
  <button type="submit">submit</submit>
</form>

JavaScript Controller: JavaScript控制器:

app.controller('ctrl', function($location) {
  $scope.username = '';
  $scope.onSubmit = function() {
    $location.url('/chat/' + $scope.username);
  };
});

Or something like that :) 或类似的东西 :)

HTML: HTML:

<form ng-submit="onSubmit()">
  <input type="text" placeholder="enter a username..." ng-model="username"  required  autofocus><br/>
  <button type="submit">submit</submit>
</form>

Controller: 控制器:

app.controller('ctrl', function($state) {
  $scope.username = 'example name';
  $scope.onSubmit = function() {
    $state.go('/chat/' + $scope.username);
  };
});

Docs: 文件:

Angular Ui 角Ui

$state.go('$scope.username') - will go to the state according to user name
$state.go('^') - will go to a parent state
$state.go('^.sibling') - will go to a sibling state
$state.go('.child.grandchild') - will go to grandchild state

Per another post on stack: 在堆栈上的另一篇文章:

the $location service is on the angular.js framework out of the box and allow you to manage location object (similar in pure javascript). $ location服务开箱即用在angular.js框架上,可让您管理位置对象(与纯JavaScript相似)。 The $state service is part of ui-router module and allow you to manage routes in an advanced mode, throughout a state machine management of views. $ state服务是ui-router模块的一部分,可让您在视图的状态机整个管理过程中以高级模式管理路由。

If you use ui-router, you should prefer to use $state service to manages states/routes because state abstracts the concept of route and you could change the phisical routes without changing states. 如果使用ui-router,则应首选使用$ state服务来管理状态/路由,因为状态抽象了route的概念,并且您可以更改物理路由而不更改状态。

Stack Post 堆叠柱

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

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