AngularJS ng-view无效

时间:2014-01-07 14:29:32

标签: javascript html angularjs

所以我遵循了这个指南:http://viralpatel.net/blogs/angularjs-routing-and-views-tutorial-with-example/

但是当我试图改变观点时,没有任何事情发生,任何人都知道我做错了什么?

这是我得到的代码。 Home.php:

<!DOCTYPE html>
<html ng-app="lax">
<head>
    <meta name="author" content="Koen Desmedt" />
    <meta name="description" content="CMS Belgium Lacrosse" />
    <meta name="keywords" content='Lacrosse, BLF, Belgium' />
    <meta name="googlebot" content="noarchive" />
    <link href="lib/bootstrap/css/bootstrap.css" rel="stylesheet">        
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js"></script>
    <script src="lax.js"></script>
    <link href="css/style.css" rel="stylesheet">        
    <title>CMS Belgium Lacrosse</title>
</head>
<body>        
    <header class="navbar navbar-inverse navbar-fixed-top bs-docs-nav" role="banner">
        <div class="container">
            <div class="navbar-header">
                <button class="navbar-toggle" type="button" data-toggle="collapse" data-target=".bs-navbar-collapse">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>                    
            </div>
            <nav class="collapse navbar-collapse bs-navbar-collapse" role="navigation">
                <ul class="nav navbar-nav navbar-left">
                    <li>
                        <a href="#/home">
                            <span class="glyphicon glyphicon-home"></span> BLF
                        </a>
                    </li>
                    <li>
                        <a href="#/players">Players</a>
                    </li>
                    <li>
                        <a href="#/club">Club</a>                            
                    </li>
                    <li>
                        <a href="#/games">Games</a>
                    </li>                        
                </ul>             
            </nav>
        </div>
    </header>
    <div id='contentcontainer'>
        <div class='container' ng-view></div>
    </div>        
</body>
</html>

lax.js:

var lax = angular.module('lax', []);

lax.config(['$routeProvider',
function($routeProvider) {
    $routeProvider.
            when('/home', {
                templateUrl: 'views/news.php',
                controller: 'NewsController'
            }).
            when('/players', {
                templateUrl: 'views/players.php',
                controller: 'PlayersController'
            }).
            otherwise({
                redirectTo: '/home'
            });
}]);

lax.controller('NewsController', function($scope) {
$scope.message = 'This is Add new order screen';
});


lax.controller('PlayersController', function($scope) {
$scope.message = 'This is Show orders screen';
});

3 个答案:

答案 0 :(得分:6)

从角度1.2.0开始,ngRoute已移至自己的模块。您必须单独加载它并声明依赖项。

更新你的html:

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

和Js:

var lax = angular.module('lax', ['ngRoute']);

有关详细信息:http://docs.angularjs.org/guide/migration

答案 1 :(得分:1)

Angular路线也需要包含route模块。 Here is the documentation涵盖了这一点。

所以,我想你可能会错过:

<script src="angular-route.js"></script>

在页面的<head>

*注意:此模块曾经是Angular的一部分,但最近被移出(1.2?)。因此,一些教程仍假设$route是内置的。

答案 2 :(得分:-1)

添加它会起作用:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular-route.js"></script>

但是第一次运行时没有互联网访问它将无法工作,因此您应该将依赖关系路由到您的项目并在您的html文件中引用

How to add ng-route dependency