Routing trouble in Angular & NodeJS [angular]

僤鯓⒐⒋嵵緔 提交于 2019-12-07 19:46:16

问题


i have a problem with angular js, i've created login.html & home.html.. after successfully login i want to change the page to home.html.

my routing is not working, default url is localhost/angular <- this display login.html after login the url changes to localhost/home, but it wont display home.html

i tried routing the "realpath" which is localhost/angular/view/home.html but still no good

my folder & file order

  • angular (folder to keep angular lib)
  • app (folder to keep app.js my routing)
  • view (folder keep my home.html)
  • login.html (as my index)
  • server.js (node.js as my server)
  • user_auth.js (function for login)

my code for login.html

<html ng-app="myApp" >
<head>
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

    <script src="angular-1.0.6/angular.js"></script>
    <script src="app/js/app.js"></script>

</head>
<title>Desainku</title>
<body>
<h2>Login</h2>

        <div  ng-controller="ajaxCtrl" >

           <p>
                <label>Username:</label>
                <input type="text" name="username" ng-model="username" />
            </p>
            <p>
                <label>Password:</label>
                <input type="password" name="password" ng-model="password" />
            </p>

            <input type="submit" value="submit" ng-click="submitPost()" />   
        <p>{{result}}</p>

</div>
</body></html>

my code for app.js (routing and send server)

var app = angular.module('myApp', []);
app.config(['$routeProvider','$locationProvider',function($routeProvider, $locationProvider) {
    $locationProvider.html5Mode(true);
    $routeProvider.when('/angular/home', {
        templateUrl: 'view/home.html',
        controller : 'homeCtrl'
    });
}]);
function homeCtrl($scope){
    alert('someone call me');
}
app.controller('ajaxCtrl', function($scope, $location) {
    var url = "http://localhost:7788/login";
    $scope.submitPost = function() {
        $.ajax({
            url: url,
            type: 'POST',
            dataType: 'json',
            data: {username: $scope.username, password: $scope.password},
            success: function(data) {
                $scope.$apply(function(){
                $scope.result=data.message;
                    if (data.status === 'true') {
                        alert($location.path());
                        $location.path('home').replace();
                }
                });
           },
            error: function(data) {
                alert('Error get data from server');
            }
        });
    };
});'

my code for server.js & user_auth.js

------------server
    var express     = require ('express'),
        app         = new express(), 
        calldb      = require ("./user_auth.js"),
        fs          = require('fs');
    app.post('/login',calldb.login);

------------user_auth

exports.login = function(req, res) {
    connDB.check_user(req.body, function(err) {
        console.log(req.header);
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "Content-Type,X-Requested-With");
        if (err) {
            res.send(JSON.stringify({status: 'false', message: 'Error login'}));

        } else {
            res.send(JSON.stringify({status: 'true', message: 'Succesfull login'}));
        }
    });
};

回答1:


User2401192,

First things first you need to declare ng-view in your html :

    <div ng-view></div>

Now when your templateUrl changes ( e.g. you execute $location.path('home').replace(); ),

$routeProvider.when('/angular/home', {
    templateUrl: 'view/home.html',
    controller : 'homeCtrl'
});

your ng-view is replaced with the content of the new url. In this case view/home.html.

Two things I'd like to add :

  1. Don't use the classic $.ajax(..), use $http service, because it has the nice advantages of promise APIs, and the extremely useful http interceptors ( might not sound like much but they are - e.g. showing a loading splash or intercepting 403 Unauthorized requests.
  2. Nothing :), there was just one thing actually.



回答2:


Replace app.js code

var app = angular.module('myApp', []);
app.config(['$routeProvider','$locationProvider',function($routeProvider, $locationProvider) {
  $locationProvider.html5Mode(true);
  $routeProvider.when('/angular/home', {
    templateUrl: 'view/home.html',
    controller : 'homeCtrl'
  });
}]);
function homeCtrl($scope){
  alert('someone call me');
}
app.controller('ajaxCtrl', function($scope, $window) {
  var url = "http://localhost:7788/login";
  $scope.submitPost = function() {
    $.ajax({
      url: url,
      type: 'POST',
      dataType: 'json',
      data: {username: $scope.username, password: $scope.password},
      success: function(data) {
        $scope.$apply(function(){
        $scope.result=data.message;
          if (data.status === 'true') {
            alert($location.path());
            //$location.path('home').replace();
            return $window.location.href = 'home.html';
           }
        });
      },
   error: function(data) {
     alert('Error get data from server');
   }
  });
 };
});

I will try it's working.



来源:https://stackoverflow.com/questions/16648934/routing-trouble-in-angular-nodejs-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!