Angular ng-view / Router无法正常工作

时间:2017-03-25 08:52:23

标签: angularjs

我是角色新手并使用Angular 1但是我总是得到404资源不可用错误 这是我正在做的代码。我正在使用ng-view而且我被困在关于为什么我的应用程序没有路由到登录或仪表板页面的部分

index.html :

<!DOCTYPE html>
<html ng-app="sampleApp" lang="en">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="webapp/js/vendor/angular.js"></script>
<script src="webapp/js/vendor/angular-route.min.js"></script>
<script src="webapp/js/app/app.js"></script>
<script src="webapp/js/app/loginController.js"></script>
<script src="webapp/js/app/dashboardController.js"></script>
<script type="text/javascript">
   angular.element(document.getElementsByTagName('head')).append(angular.element('<base href="' + window.location.pathname + '" />'));
  </script>
</head>
<body >
<ng-view></ng-view>

<a href="/login">Login</a>
<a href="/dashboard">Dashboard</a>
</body>
</html>

var sampleApp = angular.module('sampleApp',       ['ngRoute','loginModule','dashboardModule']);    sampleApp.config(['$routeProvider','$locationProvider',function($routeProvider,$locationProvider){

    $routeProvider
    .when('/login',{
        templateUrl: 'partials/login.html',
        controller: loginController
    })
    .when('/dashboard',{
        templateUrl: 'partials/dashboard.html',
        controller: dashboardController
    });

    $locationProvider.html5Mode(true);
}]);

This is my loginController.js

angular.module('sampleApp',[])
.controller('loginController',function($scope,$location){
    $scope.login = "In Login Controller";
});

dashboardController.js

angular.module('sampleApp',[])
.controller('dashboardController',function($scope,$location){
    $scope.dashboard = "In Dashboard Controller";
});

Please let me know what wrong am I doing.
Sorry for my English.

1 个答案:

答案 0 :(得分:0)

您好以下修改我已完成您的代码。 在index.html文件

旁边的partials文件夹中创建login.html
{{login}}

在index.html文件

旁边的partials文件夹中创建dashboard.html
{{dashboard}}

使用以下代码创建index.html。

<!DOCTYPE html>
<html ng-app="sampleApp" lang="en">

<head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script>
    <!--<script type="text/javascript">
        angular.element(document.getElementsByTagName('head')).append(angular.element('<base href="' + window.location.pathname + '" />'));
    </script>-->
</head>

<body>
    <ng-view></ng-view>

    <a href="/login">Login</a>
    <a href="/dashboard">Dashboard</a>
</body>

</html>
<script>
    var sampleApp = angular.module('sampleApp', ['ngRoute']);
    sampleApp.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
        $locationProvider.html5Mode({
            enabled: true,
            requireBase: false
        });
        $routeProvider.when('/login', {
            templateUrl: 'partials/login.html',
            controller: 'loginController'
        }).when('/dashboard', {
            templateUrl: 'partials/dashboard.html',
            controller: 'dashboardController'
        });
    }]);
    sampleApp.controller('loginController', function($scope, $location) {
        $scope.login = "In Login Controller";
    });
    sampleApp.controller('dashboardController', function($scope, $location) {
        $scope.dashboard = "In Dashboard Controller";
    });
</script>