Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS ng-view not working

So I followed this guide: 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:

<!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';
});
like image 903
B13ZT Avatar asked Jan 07 '14 14:01

B13ZT


Video Answer


2 Answers

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

Update your 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:

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

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

like image 87
Khanh TO Avatar answered Sep 19 '22 19:09

Khanh TO


Angular routes require the route module to be included as well. Here is the documentation that covers this.

So, I think you may be missing the:

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

In the <head> of the page.

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

like image 20
Davin Tryon Avatar answered Sep 18 '22 19:09

Davin Tryon