AngularJS 1.6 routing not working
Asked Answered
S

5

11

I am trying to create my first angular app. But its not working at all. I have no idea what's the problem. I checked the console, but there's no erros.

<head>
 <meta charset="utf-8">
 <script src="https://code.angularjs.org/1.6.0/angular.min.js"></script>
 <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular-route.js"></script>
</head>
<body ng-app="myApp">
  <h1>Test angular</h1>
  <a href="#/">Main</a>
  <a href="#sec">Second</a>
  <a href="#th">Third</a>
  <div ng-view></div>
</body>

<script>
var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider) {
  $routeProvider
  .when("/", {
      templateUrl : "main.html"
  })
  .when("/sec", {
      templateUrl : "sec.html"
  })
  .when("/th", {
      templateUrl : "th.html"
  });
});
</script>

Can anyone help me?

Sterilant answered 16/12, 2016 at 13:22 Comment(4)
there are no erros in the console :/Sterilant
Do all resources resolve properly? Do you see your template when you request you applicationurl/main.html?Stavros
Im running on local server.Sterilant
@Jaroen yes i see my template when i request toSterilant
P
30

Routes in Angular 1.6 changed from #/myUrl to #!/myUrl

You should change your ref to:

<a href="#!/">Main</a>
<a href="#!/sec">Second</a>
<a href="#!/th">Third</a>

See this answer if you want to remove this prefix.

Porkpie answered 25/1, 2017 at 13:2 Comment(2)
@John Sparwasser: Thanks. Worked for me too for 1.6.4/angular.min.js...Raper
Perfect answer !! @Porkpie great solution.Meras
R
5

Try adding $locationProvider to your script

app.config(['$locationProvider', function($locationProvider) {
        $locationProvider.hashPrefix('');
        }]);
Rumble answered 13/2, 2017 at 14:1 Comment(0)
A
1

I've found out that you didn't include the $routeProvider properly, Here's the working routing code:

app.config(['$routeProvider', function($routeProvider) {
  $routeProvider
  .when("/", {
      templateUrl : "main.html"
  })
  .when("/sec", {
      templateUrl : "sec.html"
  })
  .when("/th", {
      templateUrl : "th.html"
  });
}]);
Aspirate answered 16/12, 2016 at 13:28 Comment(3)
is the website hosted on a local server? If not, then that's the reason why it's not workingAspirate
Yes, it is hosted on local server.Sterilant
Try posting a jsFiddle with your website so that I can check why this problem is occurringAspirate
R
1

Try this code it is working

app.config(['$routeProvider','$locationProvider',function ($routeProvider,$locationProvider) {
$locationProvider.hashPrefix('');
$routeProvider
    .when('/', {
        templateUrl: 'index.html'
    })
    .when('/about', {
        templateUrl: 'about.html'
    })
    .when('/service', {
        templateUrl: 'service.html'
    });}]);
Rightly answered 22/4, 2017 at 10:52 Comment(0)
R
-3

You need to fix your href attributes:

The correct way is:

<a href="#/">Main</a>
<a href="#/sec">Second</a>
<a href="#/th">Third</a>
Rime answered 16/12, 2016 at 13:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.