ngRoute 不起作用 [英] ngRoute not working

查看:25
本文介绍了ngRoute 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脚本是:

index.html

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <title>My Application</title>
</head>
<body>
    <div ng-view></div>

    <script src="/assets/vendor/angular/angular-1.2.16.min.js"></script>
    <script src="/assets/vendor/angular/extras/angular-route.js"></script>
    <script src="/assets/myapp/myApp.js"></script>
</body>
</html>

myApp.js

(function () {
    angular.module('myApp', ['ngRoute'])
           .config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
               $routeProvider.
                 when('/', {
                     template: '<h1>Home</h1>',
                     controller: function () {
                         console.log('Home');
                     }
                 }).
                 when('/books', {
                     template: '<h1>Books</h1>',
                     controller: function () {
                         console.log('Books');
                     }
                 });
               $locationProvider.html5Mode(true);
               console.log('routes configured');
           }]);
})();

如果没有运气,我已经浪费了很多时间试图找出问题所在.我错过了一些愚蠢的东西吗?提前感谢您的帮助.

I've wasted a lot of time trying to figure out what the problem might be with no luck. Am I missing something silly? thanks in advance for the help.

推荐答案

您是否在服务器的根目录中托管您的应用程序?如果没有,那么您需要在 head 标签中使用下面的标签.

Are you hosting your application in the root of your server? If not, then you will need to use the tag below in your head tag.

<base href="PATH_HERE" />

此外,您能否将 $locationProvider.html5Mode(true) 行注释掉,然后让您的应用先在哈希路由模式下运行?

In addition, can you comment out the $locationProvider.html5Mode(true) line and get your app working in hash routing mode first?

这篇关于ngRoute 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆