简体   繁体   中英

Redirect to another page after form submit in AngularJS

I'm new to AngularJS.

I tried to submit a simple form. After getting a response from the server, I want to redirect to another page (dashboard). My url is redirected, but not the page.

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");
  });
  };
});

If I click on the submit button , my url is changed to http://localhost:8585/#!/dashboard . But I don't know how to route to the page of the dashboard. Also, in my url, why is it becoming #! ?

Thanks in advance.

您可以使用以下内容:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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