【问题标题】:Redirect to another page after form submit in AngularJS在AngularJS中提交表单后重定向到另一个页面
【发布时间】:2017-11-23 18:40:04
【问题描述】:

我是 AngularJS 的新手。

我尝试提交一个简单的表单。从服务器获得响应后,我想重定向到另一个页面(仪表板)。我的 url 被重定向了,但页面没有被重定向。

index.html

<!DOCTYPE html>
<html ng-app="myApp">
  <head>
    <meta charset="utf-8">
    <title>index page</title>
    <script src="bower_components/angular/angular.min.js"></script>
    <script src="bower_components/angular-route/angular-route.min.js">
</script>
    <script type="text/javascript" src="front/controller/controller.js">
</script>
  </head>
  <body>
    <div ng-controller="myCtrl">
      <p>Today's welcome message is:</p>
      <form novalidate>
        <input type="text" ng-model="name">
        <input type="password" ng-model="passwww">
        <button type="button" ng-click="btn()" name="button">submit</button>
      </form>
    </div>
  </body>
</html>

Controller.js

var app = angular.module('myApp', []);
app.controller('myCtrl',function ($scope,$http,$location) {
  $scope.btn = function () {
    var nam = $scope.name;
    var pass = $scope.passwww;
    var detail ={
      "name" : nam,
      "password" : pass
      }
      console.log(nam + " and1 " + pass);
   $http({
     url : "/daaa",
     method : "post",
     data : detail,
     headers : {'Content-Type' : 'application/json'}
   })
  .then(function (response) {
     $location.path('/dashboard');
     console.log( $location.path());
  }).catch(function (response) {
    console.error("error in posting");
  });
  };
});

如果我点击submit 按钮,我的网址将更改为http://localhost:8585/#!/dashboard。但我不知道如何路由到仪表板的页面。另外,在我的网址中,为什么会变成#!

提前致谢。

【问题讨论】:

  • 你应该考虑使用 ngRoute
  • 编辑和分享代码。会有帮助的。

标签: javascript html angularjs express angular-routing


【解决方案1】:

您可以使用以下内容:

<form onsubmit="window.location.href='type://your.url/here';" novalidate>

【讨论】:

    【解决方案2】:

    您可以直接在javascript中设置window.location = 'newurl'

    【讨论】:

      猜你喜欢
      • 2017-06-26
      • 1970-01-01
      • 2013-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-08
      相关资源
      最近更新 更多