1

I have a problem with my code:

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

app.config( function ( $routeProvider ) {
  $routeProvider
    .when( '/home', { templateUrl: 'strony/home.html' } )
    .when( '/about', { templateUrl: 'strony/about.html' } )
    .when( '/contact', { templateUrl: 'strony/contact.html' } )
    .otherwise( { redirectTo: '/this' } );
});

app.controller( 'MainCtrl', function ( $scope ) {
});
<div ng-controller="MainCtrl">
  <ul>
    <li><a href="#/home">Home</a></li>
    <li><a href="#/about">About Us</a></li>
    <li><a href="#/contact">Contact Us</a></li>
  </ul>
  <div ng-view></div>
</div>

Path looks like: -index.html -script.js -strony/ *home.html *about.html *contact.html

If i click on link adress name is changing but on site nothing happenes. Can you help me with this?

Ernest
  • 33
  • 3

1 Answers1

0

Add a forward slash in front of "strony" (i.e.'/strony/home.html') and make sure your are referencing your module (i.e. <html ng-app='myApp'>)

Your code will reflect the following:

HTML

 <html ng-app='myApp'>
    <head>
    </head>
    <body>
    <div ng-controller="MainCtrl">
      <ul>
        <li><a href="#/home">Home</a></li>
        <li><a href="#/about">About Us</a></li>
        <li><a href="#/contact">Contact Us</a></li>
      </ul>
      <div ng-view></div>
    </div>
    </body>
    </html>

Angular

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

app.config( function ( $routeProvider ) {
  $routeProvider
    .when( '/home', { templateUrl: '/strony/home.html' } )
    .when( '/about', { templateUrl: '/strony/about.html' } )
    .when( '/contact', { templateUrl: '/strony/contact.html' } )
    .otherwise( { redirectTo: '/this' } );
});

app.controller( 'MainCtrl', function ( $scope ) {
});

Here's a working Plunk

Also, ng-route needs a web server to work. Maybe use something like http-server.

Culpepper
  • 1,093
  • 12
  • 19
  • still doesn`t work. in head i have scripts angular.min.js and angular-route.min.js – Ernest Jun 02 '16 at 21:43
  • still nothing. I have ng-app='myApp' in HTML and i change 'strony/home.html' to '/strony/home.html'. One more information, when i click on link in adress page is changing but on site is nothing happenes – Ernest Jun 02 '16 at 21:50
  • Yes now in your Plunk it is working but if in my code in sublime still doesn`t work and i don`t know why ;/ have you any idea? mabye i must enter localhost? And okay i try to use local server thanks for help :) – Ernest Jun 02 '16 at 22:03
  • I had to strat it by localhost. Now everything is working ;) – Ernest Jun 05 '16 at 20:31