3

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
  • Instead of $location.path('home').replace(), could you simply write $location.path('/angular/home')` and check if it works? Commented May 24, 2013 at 8:13

2 Answers 2

4

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.
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.