Angular ng-href doesn't work

5.4k Views Asked by At

I am using Chrome and have set up node server that is listening on port 8080 and provides all listed files. Angular app.js suppose to show the content of StuffView.html (simple text).

index.html :

<!doctype html>
<html ng-app="app">
    <head>
        <meta charset="utf-8">
        <title>angular</title>
        <link rel="icon" href="">
        <link rel='stylesheet' href="css/style.css">
        <script src="libs/angular/angular.js"></script>
        <script src="libs/angular-route/angular-route.js"></script>
        <script src="app.js"></script>
        <script src="js/controllers/StuffController.js"></script>
    </head>
    <body>
        <div>
            <a ng-href = "#/stuff"> show stuff </a>
            <ng-view></ng-view>
        </div>
    </body>
</html>

app.js:

angular
.module( 'app', [ 'ngRoute' ] )
.config( function( $routeProvider ) {

    $routeProvider.when( '/stuff', {
        templateUrl : "views/StuffView.html",
        controller : "stuffController"
    } );

} );

StuffView.html:

<H1>Hello from Stuff View! {{hello}}</H1>

where {{hello}} comes from stuffController :

angular
.module( 'app' )
.controller( 'stuffController', stuffController );

function stuffController( $scope ) {
    $scope.hello = 'Hello from stuffController! ';
}

When I click the link, the address in browser changes to http://localhost:8080/#!#%2Fstuff and nothing gets displayed. No errors in the console. What am I missing?

2

There are 2 best solutions below

6
Andriy On BEST ANSWER

please refer to this link for ngRoute module use. Do not use 'ng-href' directory in your static case, instead try:

<a href = "#/stuff"> show stuff </a>
2
Jalpesh Vadgama On

I think you have used ng-href in wrong way see the angular documentation for ng-href for the same.

https://docs.angularjs.org/api/ng/directive/ngHref

You should create a variable called path for pathforstuffand then use that variable like below.

var pathforstuff="stuff";

<a ng-href="#/{{pathforstuff}}"/>Take Me Somewhere</a>