1

I'm creating a web portfolio and the main page has a menu. When the user clicks 'video' I want the site to go to mywebsite.com/video and go to video.html. I'm using AngularJS and I can't figure out what I'm doing wrong.

index.html

<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript" src="js/main.js"></script>
</head>
<body ng-app="myApp">
<nav class="topnav" id="myTopnav">
    <a href="#portfolio">Portfolio</a>
    <a href="#events">Upcoming Events</a>
    <a href="#cv">CV</a>
    <a href="#video">Video Reel</a>
</nav>
<div class="ng-view"></div>
</body>

main.js

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

app.config(function($routeProvider) {

$routeProvider
    .when(
    '/', {
        redirectTo: '/home'
    })
    .when('/portfolio', {
        templateUrl: 'templates/portfolio.html'
    })
    .when('/events', {
        templateUrl: 'templates/events.html'
    })
    .when('/cv', {
        templateUrl: 'templates/cv.html'
    })
    .when('/video', {
        templateUrl: 'templates/video.html'
    })
    .otherwise({ redirectTo: '/' });
});

3 Answers 3

1

can you try to add $locationProvider service into config and change href attributes

//html
<body ng-app="myApp">
<nav class="topnav" id="myTopnav">
    <a href="#/portfolio">Portfolio</a>
    <a href="#/events">Upcoming Events</a>
    <a href="#/cv">CV</a>
    <a href="#/video">Video Reel</a>
</nav>
<div class="ng-view"></div>
</body>

//js
app.config(['$routeProvider','$locationProvider',
  function($routeProvider,$locationProvider) {

$routeProvider
    .when(
    '/', {
        redirectTo: '/'
    })
    .when('/portfolio', {
        templateUrl: 'templates/portfolio.html'
    })
    .when('/events', {
        templateUrl: 'templates/events.html'
    })
    .when('/cv', {
        templateUrl: 'templates/cv.html'
    })
    .when('/video', {
        templateUrl: 'templates/video.html'
    })
    // removed other routes ... *snip
    .otherwise({
        redirectTo: '/'
    }
);

// enable html5Mode for pushstate ('#'-less URLs)
//$locationProvider.html5Mode(true);
//$locationProvider.hashPrefix('!');
}]);
Sign up to request clarification or add additional context in comments.

4 Comments

don't get the error, but clicking 'Video Reel' also doesn't work, the url changes, but the page stays the same
do you have video.html?
my urls are messed up as well, when I click on "Video Reel" i get localhost:8000/#!/#video
1

It should be,

 <a href="#/portfolio">Portfolio</a>
 <a href="#/events">Upcoming Events</a>
 <a href="#/cv">CV</a>
 <a href="#/video">Video Reel</a>

DEMO

7 Comments

@PankajParkar where?
I tried that and it still isn't working. I'm also getting: angular.js:38 Uncaught Error: [$injector:modulerr]
@Kat Then check your dependencies are properly injected, check the demo
commented out $locationProvider as that was causing the errors, but clicking the 'video' link still does nothing
@Kat did you check the attached demo
|
1

I think you should inject $locationProvider because you are using it but I do not see the injection.

1 Comment

thank you, i commented out $locationProvider for now

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.