【问题标题】:ngRoute not displaying my viewsngRoute 不显示我的视图
【发布时间】:2017-06-12 16:36:09
【问题描述】:

我正在尝试使用 ngRoute 将我的网站路由到登录页面,但它在构建后没有加载任何 html,我启动了我的 grunt 服务器。当我尝试加载页面时,我的控制台中也没有出现任何错误。我可以看到所有文件都被正确复制到'dist 文件夹中,但是当我启动服务器时,它会将我带到http://localhost:9000,然后只显示一个空的 index.html 页面。我究竟做错了什么?

编辑:我已经从我的配置中删除了 $scope,但仍然没有看到我的登录页面。当我将 $scope 变量直接放在 index.html 文件的正文中时,我可以在控制器中显示它们,如果这有助于缩小问题范围。

插件链接:https://plnkr.co/edit/wagbCGdSx5lRO93u5jCF?p=info

这是我的 myWebsite.js

/*global angular*/
(function (angular) {
    'use strict';
    angular.module('myWebsite', ['myWebController', 'ngRoute']);
}(angular));  

和控制器

(function (angular) {
'use strict';
angular.module('myWebController', [])
    .controller('myWebController', ['$scope', function ($scope) {
    }]);
} (angular));

还有我的路由器

/*global angular:true */
(function (angular) {
    'use strict';
    angular.module('pageRouter', ['ngRoute'])
    .config(['$scope', '$routeProvider', '$locationProvider', function ($scope, $routeProvider, $locationProvider) {
        $routeProvider
            .when('/', {
                templateUrl: 'markup/landing-page.html',
                controller: 'myWebController'
            })
            .when('', {
                templateUrl: 'markup/landing-page.html',
                controller: 'myWebController'
            })
            .otherwise({
                templateUrl: 'markup/landing-page.html',
                controller: 'myWebController'
            });

        $locationProvider.html5Mode({enabled: true});
    }]);
} (angular));

这里是 index.html

<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <base href="/"/>
</head>
<body>
    <div ng-app="myWebsite" ng-controller="myWebController">
        <div ng-view></div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"> </script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-route.js"></script>
    <script src="js/myWebsite.js"> </script>
</body>
</html>

最后是我的 gruntfile。我有“构建”任务将我所有的 js 文件连接成一个并将其放在 dist/js/ 中。标记被复制到 dist/markup/ 并且 index.html 文件被复制到 dist/。然后我在我的 grunt 服务器上提供 dist 文件夹。

module.exports = function (grunt) {

var distPath = 'dist/';

grunt.initConfig({

    jshint: {
        files: [ 'src/**/*.js'],
        options: {
            globals: {
                jQuery: true
            }
        }
    },
    watch: {
        files: ['<%= jshint.files %>'],
        tasks: ['jshint']
    },
    clean: {
        dist: ['dist']
    },
    connect: {
        server: {
            options: {
                keepalive: true,
                port: 9000,
                base: 'dist',
                open: true
            }
        }
    },

    concat: {
        js: {
            src: ['src/main/**/*.js'],
            dest: distPath + '/js/myWebsite.js'
        }
    },
    copy: {
        html: {
            cwd: 'src',
            expand: true,
            src: '*.html',
            dest: distPath + 
        },
        markup: {
            cwd: 'src/main/markup',
            expand: true,
            src: '*.html',
            dest: distPath + '/markup'
        },
        images: {
            cwd: 'src/main/images',
            expand: true,
            src: '*.png',
            dest: distPath + '/images'
        }
    }
});

grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-concat');

grunt.registerTask('default', ['jshint', 'connect:server']);
grunt.registerTask('devServer', ['build', 'connect:server']);
grunt.registerTask('build', ['clean:dist','jshint', 'concat:js', 'copyDist']);
grunt.registerTask('copyDist', ['copy:html', 'copy:markup', 'copy:images']);

};

【问题讨论】:

  • 试试.otherwise({redirectTo: 'markup/landing-page.html'});
  • 我添加了这个,但我仍然看到一个空白页。
  • 您的ng-view 是空白的,预计会看到一个空白页面。将您的 ng-view 重定向到您的目标网页。在处理ng-view 的第一个控制器中使用$location.path("/index");,然后在路由提供程序中设置登录页面和新控制器。如果这对您有用,我会将其作为实际答案发布。
  • 很抱歉,我无法理解您的意思。我唯一的控制器是 myWebController。所以你是说将 ng-route 添加到依赖项中,注入 $location,然后在控制器中执行 $location.path("/index")?我刚刚尝试过,它在我的浏览器中将我的路径更改为“localhost:9000/#!/index”,但仍然显示空白页。

标签: html angularjs server gruntjs ngroute


【解决方案1】:
You are trying to inject *$scope* in the config function, which is causing the error. 

Below is the snippet after removing $scope.

    angular.module('trial',[]).config((['$scope', '$routeProvider', '$locationProvider', function ($scope, $routeProvider, $locationProvider) {
    ....
    }));

https://plnkr.co/edit/lgc15G2gadoa5WVYzLH0?p=preview

When you see a blank page, you can open developer tool of the browser and see the console to debug. Make sure you have selected *Show all messages* on the Chrome developer console, which will show you errors

我使您的代码版本也可以正常工作。我修复的一些问题描述如下:

  1. 更改要动态获取的基本 URL。
<script>
  document.write('<base href="' + document.location + '" />');
</script>
  1. 您没有导入另外两个脚本文件。我在您的 index.html 中添加了以下条目
<script src="myWebController.js"></script>
<script src="pageRouter.js"></script>
  1. pageRouter 模块添加为您的主 myWebsite 模块的依赖项。
angular.module('myWebsite', ['myWebController', 'pageRouter']);

工作示例链接:https://plnkr.co/edit/TzkzpJZlfL2XBWV8mUQ9?p=preview

【讨论】:

  • 我从配置函数中删除了 $scope,但我仍然看到一个空白页。当我在 chrome 中打开调试工具时,控制台中没有错误。我可以看到我直接放在 index.html 文件正文中的任何内容,但路由器似乎没有路由到登录页面。
  • 你能在 Github 上创建一个 plunker 页面或分享代码让我看看吗?
  • 这里是我用我所有的代码制作的一个 plunker 的链接。 plnkr.co/edit/wagbCGdSx5lRO93u5jCF?p=info
  • 确保您在 Chrome 开发者控制台上选择了显示所有消息,这将显示错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-17
  • 2017-12-31
  • 1970-01-01
相关资源
最近更新 更多