简体   繁体   English

角js路线不工作

[英]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 的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 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 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 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. 您不必在部分模板中重复角度ng-appng-view ng-controller="dashBoardController"指令。 Change dashboard.html like this: 像这样更改dashboard.html

<h1>demo</h1>

However in index.html you have to put ngView somewhere. 但是在index.html你必须把ngView放在某个地方。 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 演示: http //plnkr.co/edit/Is3oXmkcRvDYkiAuvnCF?p = preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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