角js路线不工作

时间:2015-03-22 17:41:28

标签: javascript angularjs angular-routing

我有以下代码。我正在尝试的是制作一个简单的登录和仪表板应用程序。成功登录后,用户可以看到仪表板,我有两条路线登录和仪表板初始登录路线是默认的一旦登录成功然后我想路由到仪表板路线,相应的模板和控制器应该加载但不知何故不起作用。我需要帮助才能找出问题所在

的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>

但登录和仪表板路线都没有被解雇,任何人都可以帮我找出我做错了什么

1 个答案:

答案 0 :(得分:6)

您不必在部分模板中重复角度ng-appng-view ng-controller="dashBoardController"指令。像这样更改dashboard.html

<h1>demo</h1>

但是在index.html中你必须把ngView放在某个地方。它将填充加载的模板:

<div ng-view></div>

使用固定代码检查演示:

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