Javascript 在表单提交时发送带有URL的路由图

Javascript 在表单提交时发送带有URL的路由图,javascript,html,angularjs,angularjs-routing,Javascript,Html,Angularjs,Angularjs Routing,当用户单击提交按钮时,我需要发送带有URL的路由参数 这是我的简单表格 <form action="#/chat/{{username}}"> <input type="text" placeholder="enter a username..." ng-model="username" required autofocus><br/> <input type="submit" value="submit"> </for

当用户单击提交按钮时,我需要发送带有URL的路由参数

这是我的简单表格

<form action="#/chat/{{username}}">
    <input type="text" placeholder="enter a username..." ng-model="username" required  autofocus><br/>
    <input type="submit" value="submit">
  </form>
但上述方法的问题是,当用户按下ENTER键时(因为它不是submit按钮),它不起作用

那么,实现这一目标的正确方法是什么呢?

标记:

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

HTML:

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

根据堆栈上的其他帖子:

$location服务位于angular.js框架的开箱即用中,允许您管理位置对象(类似于纯javascript)。$state服务是ui路由器模块的一部分,允许您在视图的状态机管理中以高级模式管理路由

如果您使用ui路由器,您应该更喜欢使用$state service来管理状态/路由,因为状态抽象了路由的概念,您可以在不更改状态的情况下更改物理路由


尝试在angularJS范围内使用ng submit处理表单提交
app.controller('ctrl', function($location) {
  $scope.username = '';
  $scope.onSubmit = function() {
    $location.url('/chat/' + $scope.username);
  };
});
<form ng-submit="onSubmit()">
  <input type="text" placeholder="enter a username..." ng-model="username"  required  autofocus><br/>
  <button type="submit">submit</submit>
</form>
app.controller('ctrl', function($state) {
  $scope.username = 'example name';
  $scope.onSubmit = function() {
    $state.go('/chat/' + $scope.username);
  };
});
$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