简体   繁体   中英

angular js route not working

I have following code.All I am trying is to make a simple login and dashboard app. after successful login the user can see dashboard for that I have two routes login and dashboard initially login route is default once login is successful then I want to route to dashboard route and the respective template and controller should get loaded but somehow that does not work. I need help to figure out what is wrong

index.html

<html lang="en" ng-app="salesApp">
<body ng-controller="loginController">
    <form name="loginForm">
        <!--login form-->
    </form>
    <script src="./js/lib/angular.min.js"></script>
    <script src="./js/lib/angular-route.min.js"></script>
    <script src="./js/app/salesApp.js"></script>
    <script src="./js/controller/loginController.js"></script>
    <script src="./js/services/communicationService.js"></script>

</body>
</html>

loginController.js

app.controller('loginController', function($scope, $http, communicationService, $location){
    $scope.isLoginFailed = false;
    $scope.login= function(){
        $http.get("http://localhost:8080/login?username=" + $scope.user.username + "&password=" + $scope.user.password)
        .success(function(response){
            if(response.sessionId && response.loginSucceeded){
                $location.path("/login");
            }else{
                $scope.isLoginFailed = true;
            }
        });
    }
});

salesApp.js

 var app = angular.module("salesApp", ['ngRoute']);
app.config(['$routeProvider',
        function($routeProvider) {
            debugger;
            $routeProvider.
                when('/login', {
                    templateUrl: '../../login.html',
                    controller: 'loginController'
                }).
                when('/dashboard', {
                    templateUrl: '../../dashboard.html',
                    controller: 'dashBoardController'
                }).
                otherwise({
                    redirectTo: '/login'
                });
        }]);

app.controller('dashBoardController', function($scope, $http, communicationService){
    $scope.sessionData = communicationService.getSessionData();
    $scope.isValidSession - $scope.sessionData.sessionId !== null ? true : false;
    $scope.isValidSession = $scope.isValidSession && $scope.sessionData.loginSucceeded;
}); 

dashboard.html

<html ng-app="salesApp">
<head>
</head>
<div ng-view ng-controller="dashBoardController">
<h1>
demo
</h1>
</body>

but neither login nor dashboard route gets fired, Can any one help me to find out what wrong am I doing

You don't have to repeat angular ng-app and ng-view ng-controller="dashBoardController" directives in partial templates. Change dashboard.html like this:

<h1>demo</h1>

However in index.html you have to put ngView somewhere. It will be filled with loaded templates:

<div ng-view></div>

Check the demo with fixed code:

Demo: http://plnkr.co/edit/Is3oXmkcRvDYkiAuvnCF?p=preview

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