简体   繁体   中英

AngularJS route with routeprovider not working

I have this index.html page:

<html>
<head ng-app="myApp">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Test App/title>
    <link rel="stylesheet" href="app.css">
</head>
<body ng-controller="mainController">
    <a href="#/index2">index2</a>
    <div ng-view></div>
    <script src="vendor/angular/angular.js"></script>
    <script src="vendor/angular-route/angular-route.js"></script>
    <script src="app.js"></script>
</body>
</html>

And this is my app.js script:

'use strict';

var myApp = angular.module('myApp', ['ngRoute'])
    .config(function ($routeProvider) {
        $routeProvider
            .when('/index2', {
                templateUrl: 'views/index2.html'
            })
            .otherwise({redirectTo: '/views/index2.html'});
    });
myApp.controller('mainController', function ($scope) {
});

And my views/index2.html is contains only single 123 element. When I'm clicking link on my index.html, nothing happens, no routing. What I'm doung wrong?

upd: I run it from IntelliJ IDEA so it's nothing about webserver is missing ,I think.

You have ng-app directive applied to your head tag:

<head ng-app="myApp">

This means that ng-app will apply to head tag only. Instead you need to move it to your html tag:

<html ng-app="myApp">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM