1

I came across with Angular routing example when I'm following a tutorial. I tried as the tutorial but it won't work. But when I copied the code from the tutorial it works perfectly. Can anyone help me with this.

This is my main page

<div ng-controller="routingCtrl">
    <div>
        <ul>
            <li><a href="#home">Home</a> </li>
            <li><a href="#other">Oher</a></li>
            <li><a href="#contact">Contact</a></li>
        </ul>
        <div>
            <p>{{message}}</p>
        </div>
        <ng-view></ng-view>
    </div>
</div>

And this is my script file called script.js

    var routingApp = angular.module('sampleRouting', ['ngRoute'])
              .controller('routingCtrl', function ($scope) {
                  $scope.message = "Main Page";
              })
              .config(function ($routeProvider, $locationProvider) {
                  $routeProvider
                  .when('/', {
                      templateUrl: 'home.html',
                      controller: 'homeCtrl'
                  })
                  .when('/other', {
                      templateUrl: 'other.html',
                      controller: 'otherCtrl'
                  })
                  .when('/contact', {
                      templateUrl: 'contact.html',
                      controller: 'contactController'
                  })
                  .otherwise({ redirectTo: '/' }); 
                  })
                  .controller('homeCtrl', function ($scope) {
                      $scope.homeMessage = "Welcome to home !"
                  })
                  .controller('otherCtrl', function ($scope) {
                      $scope.otherMessage = "Welcome to other !"
                  })
                  .controller('contactController', function ($scope) {
                      $scope.contactMessage = "Welcome to Contacts !"
                  })

And this is other.html

    <div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

Other two home.html and contact.html also contains random texts like this.

The problem is home.html view has loaded into the index.html at the beginning of the but when I clicked hyperlinks to Other and Contact the content won't chance. As the home.html has loaded I think ngRoute works well. But other two won't work. Can anyone help me to solve this issue? Thanks a lot.

2 Answers2

0

It's probably because you are using angular 1.6 and the tutorial are using a older version of angular and that's why it works when you copy the code.

You can verify that this is the case by changing:

<li><a href="#other">Oher</a></li>

to

<li><a href="#!other">Oher</a></li>

There has been a change in the default hash-prefix in 1.6. For possible solution to this problem take a look at: AngularJS: ngRoute Not Working

If you want to revert it back to the tutorial version you should use option 3 from the link and manually set the hash-prefix.

app.config(['$locationProvider', function($locationProvider) {
  $locationProvider.hashPrefix('');
}]);
Community
  • 1
  • 1
Anders Vestergaard
  • 1,139
  • 1
  • 14
  • 21
  • Can you please tell me what's wrong with my smart table? Everything is working fine except routing [check this](https://plnkr.co/edit/hb3gcgp6snBPLCA9I8qw?p=preview) –  Feb 06 '17 at 00:12
0

In your config try following for removing routing with #

$locationProvider.html5Mode(true);

In your html remove #

<li><a href="home">Home</a> </li>
<li><a href="other">Oher</a></li>
<li><a href="contact">Contact</a></li>
  • I tried this. But didn't work. Anyway thanks a lot for the help –  Feb 05 '17 at 11:18
  • You should configure your server side code to make this work. See this answer http://stackoverflow.com/questions/42046583/cant-get-route-angular/42046776#42046776 – Salih Şenol Çakarcı Feb 05 '17 at 11:20