javascript AngularJS ng-view 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20974214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:44:40  来源:igfitidea点击:

AngularJS ng-view not working

javascripthtmlangularjs

提问by B13ZT

So I followed this guide: http://viralpatel.net/blogs/angularjs-routing-and-views-tutorial-with-example/

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

But when I try to change the view nothing happens, anybody a idea what I do wrong?

但是当我试图改变观点时什么也没有发生,有人知道我做错了什么吗?

This is the code I got. Home.php:

这是我得到的代码。主页.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:

松弛.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';
});

采纳答案by Khanh TO

From angular 1.2.0, ngRoute has been moved to its own module. You have to load it separately and declare the dependency.

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

Update your html:

更新您的 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>

And Js:

和JS:

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

For more information: http://docs.angularjs.org/guide/migration

更多信息:http: //docs.angularjs.org/guide/migration

回答by Davin Tryon

Angular routes require the routemodule to be included as well. Here is the documentationthat covers this.

Angular 路由也需要route包含该模块。 这是涵盖此内容的文档

So, I think you may be missing the:

所以,我认为你可能会错过:

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

In the <head>of the page.

<head>页面的。

*Note: this module used to be part of Angular, but was moved out recently (1.2?). So, some tutorials are still assuming that $routeis built-in.

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

回答by Ashif

Adding this would work :

添加这个会起作用:

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

but it will not work without internet access when running for a first time ,Therefore you should ng-route dependency to your project and refer that in your html file

但是在第一次运行时它不会在没有互联网访问的情况下工作,因此你应该 ng-route 对你的项目的依赖并在你的 html 文件中引用它

How to add ng-route dependency

如何添加 ng-route 依赖项