2

I am new to AngularJs and I just started learning about routing and partial templates. I'm writing a little application that is trying to do routing and inserting a partial templates into the layout HTML page when you click on a specific hyperlink such as Home, about or contact.

However, I'm having difficulty understanding why my templates are not loading into the layout view. I researched Stack Overflow and other resources and nothing seems to work. The structure of my files is like this:

index.html file
Templates Folder:
  +home.html
  +about.html

+contact.html
Scripts folder:
  +script.js
  +angular-route.min

Following is my code: index.html:

<!--index.html-->
<!DOCTYPE html>

<html>

<head>
<title>Routing App Example</title>

<script src = 
"https://code.angularjs.org/1.5.6/angular.js">`
</script>`

<script src = "https://code.angularjs.org/1.5.6/angular-route.js">`</script>
<script type="text/javascript" src = "Scripts/script.js"></script>
</head>

<body ng-app = "RoutingApp" ng-controller = "MainController">

<header>
    <div>
        <ul>
            <li><a href = "#/home">Home</a></li>
            <li><a href = "#/about">About</a></li>
            <li><a href = "#/contact">Contact</a></li>
        </ul>
    </div>
</header>
    <div>
        index ng-root
        <ng-view></ng-view>
    </div>
</div>
</body>
</html>

script.js:

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

RoutingApp.config(['$routeProvider', '$locationProvider', '$scope',   function($routeProvider, $locationProvider){

$routeProvider
// route for the index page

// route for the addOrder page

.when('/home', {
    templateUrl: 'Templates/home.html',
    controller: 'HomeController'
})
.when('/about', {
    templateUrl: 'Templates/about.html',
    controller: 'AboutController'
})
// route for the showOrders page
.when('/contact', {
    templateUrl: 'Templates/contact.html',
    controller: 'ContactController'
})
.otherwise({
    redirectTo: '/home',
});

}]);

RoutingApp.controller('MainController', function($scope){
$scope.message = "Welcome from the MainController";
});

RoutingApp.controller('HomeController', function($scope){
    $scope.message = "Welcome from the HomeController";$scope
});

RoutingApp.controller('AboutController', function($scope){
    $scope.message = "Welcome from the AboutController";
});

RoutingApp.controller('ContactController', function($scope){
    $scope.message = "Welcome from the ContactController";
});

home.html

<h1>{{message}}</h1>

about.html

<h1>{{message}}</h1>

contact.html

<h1>{{message}}</h1>

The problem that I'm having is the fact that the partial templates are not loading into the layout view page. I don't know what I'm doing wrong.

13
  • Is your partial template files are in same folder as index.html or they are in Template folder as you mentioned Commented Jun 12, 2016 at 18:01
  • No the partial templates are in a seperate folder called Templates. Commented Jun 12, 2016 at 18:34
  • you can check console for errors Commented Jun 12, 2016 at 18:50
  • I checked the Chrome console errors. This is what I'm getting: Commented Jun 12, 2016 at 20:09
  • SCRIPT5022: [$injector:modulerr] errors.angularjs.org/1.5.6/$injector/modulerr Commented Jun 12, 2016 at 20:21

1 Answer 1

1

You were getting that error because

  1. In your route config locationProvider should be $locationProvider.
  2. You are not injecting scope, but using it in route config. I have a doubt that it even exist for route config.

and some more typos.

correct route config is :

RoutingApp.config( [ '$routeProvider', '$locationProvider', function( $routeProvider, $locationProvider ){


    $routeProvider
      .when('/home', {
            templateUrl: 'template/home.html',
            controller: 'HomeController'
        })
        .when('/about', {
            templateUrl: 'template/about.html',
            controller: 'AboutController'
        })
        .when('/contact', {
            templateUrl: 'template/contact.html',
            controller: 'ContactController'
        })
        .otherwise({
            redirectTo: '/home',
         });

    } ] );

Here's a modified version of you code :
https://plnkr.co/edit/CtMucCAfuxb8JHO5Buci?p=preview

Sign up to request clarification or add additional context in comments.

2 Comments

thank you for your help. I did made all the code changes that you suggested. When I run the app, it doesn't give me any errors on the console. However, the data expression is not binding. So what I get is ""{{message}} on every link that I click on.
@MoLaiouny is plunker example not working for you?. Please update your question with your new code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.