繁体   English   中英

AngularJS路由无法按预期工作

[英]AngularJS Routing Not Working As Expected

我是AngularJS的新手,现在迷路了。 我正在尝试从以下代码中学习: http : //plnkr.co/edit/dd8Nk9PDFotCQu4yrnDg?p=preview

的script.js:

// create the module and name it scotchApp
var scotchApp = angular.module('scotchApp', ['ngRoute']);

// configure our routes
scotchApp.config(function($routeProvider) {
    $routeProvider

        // route for the home page
        .when('/', {
            templateUrl : 'pages/home.html',
            controller  : 'mainController'
        })

        // route for the about page
        .when('/about', {
            templateUrl : 'pages/about.html',
            controller  : 'aboutController'
        })

        // route for the contact page
        .when('/contact', {
            templateUrl : 'pages/contact.html',
            controller  : 'contactController'
        });
});

// create the controller and inject Angular's $scope
scotchApp.controller('mainController', function($scope) {
    // create a message to display in our view
    $scope.message = 'Everyone come and see how good I look!';
});

scotchApp.controller('aboutController', function($scope) {
    $scope.message = 'Look! I am an about page.';
});

scotchApp.controller('contactController', function($scope) {
    $scope.message = 'Contact us! JK. This is just a demo.';
});

我的index.html文件包括

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>

我将其设置在本地环境中。 我可以加载主页,但路由只会更改URL,而没有其他更改。 任何想法可能是什么原因造成的?

当您说仅调用Url而未呈现页面时,ngroute可以正常工作。 您需要确保的一件事是所有html文件都位于pages目录下。

在index.html页面内也应该有<ng-view>标记

最好的例子是您所指的pl子。

我找到了解决我问题的方法。 我必须添加

app.use(express.static(__dirname ));

到我的节点服务器脚本。 不过,我不明白为什么需要这样做。

暂无
暂无

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

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