user3629892
user3629892

Reputation: 3046

angular.js - problems getting routing to work

This is my code:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<style>
</style>
</head>
<body ng-app="myApp" >

<div data-role="page" id="pageone">
  <div data-role="header">
    <h1>WELCOME!</h1>
  </div>

<div ng-view></div>

    <div data-role="footer">
    <h1>&copy; 2015 - HSKA</h1>
  </div>
</div>

<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script>
'use strict';

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

app.config(function($routeProvider) {
    $routeProvider
        .when('/', {
            templateUrl : 'views/home.html',
            controller  : 'HomeController'
        })
        .when('/abc', {
            templateUrl : 'views/abc.html',
            controller  : 'abcController'
        })
        .when('/def', {
            templateUrl : 'views/def.html',
            controller  : 'defController'
        })
        .when('/efg', {
            templateUrl : 'views/efg.html',
            controller  : 'efgsController'
        });

});

app.controller('HomeController', function($scope, $http, $location) {

     $http.get('http://localhost/webapp/dbconnect.php').
        success(function(data) {
            $scope.users = data;
        }).error(function(data) {
            console.log("error");
        });

});

app.controller('abcController', function($scope) {
    $scope.info = 'abcController';
});

app.controller('defController', function($scope) {
    $scope.info = 'defController';
});

app.controller('efgsController', function($scope) {
    $scope.info = 'efgsController';
});

</script>

</body>
</html>

This is how e. g. the efg.html looks:

<div data-role="main" class="ui-content"  ng-controller="efgsController">
    <h1>VG</h1>
    <p>{{ info }}</p>
</div> 

However, I get the following error:

Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.3.14/$injector/modulerr?p0=myApp&p1=Error%3A%…gleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.14%2Fangular.min.js%3A17%3A381)

What exactly am I doing wrong? Am I using an outdated way to implement this?

Help and tips would be highly appreciated!

Upvotes: 0

Views: 86

Answers (3)

Sridhar Gudimela
Sridhar Gudimela

Reputation: 584

you have to include angular-route.js which is manadatory for versions after 1.2.0 in your header section after your angular.min.js inclusion

Upvotes: 1

Sani Huttunen
Sani Huttunen

Reputation: 24395

All modules must be referenced and since you are including ngRoute you need to reference the corresponding source.

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

Upvotes: 1

TimCodes
TimCodes

Reputation: 365

You have to include angular route as a separate file so under where add the angular core script tag add this, or use a cdn of your choice

  <script src= "https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.14/angular-route.js"></script>

Upvotes: 1

Related Questions