简体   繁体   English

无法使用AngularJS routeProvider重定向到某些页面

[英]Unable to redirect to some pages using AngularJS routeProvider

I'm trying to code a spring boot application based in Angularjs. 我正在尝试编写基于Angularjs的Spring Boot应用程序。 I have a project structure as follows: 我的项目结构如下:

项目结构Eclipse] [1

The controller calls the index.html perfectly: 控制器完美地调用index.html:

调节器

the app.js: app.js:

var app = angular.module('app', ['ngRoute','ngResource']);
app.config(function($routeProvider){
    $routeProvider
        .when('/users',{
            templateUrl: 'views/users.html',
            controller: 'usersController'
        })
        .when('/roles',{
            templateUrl: 'views/roles.html',
            controller: 'rolesController'
        })
        .otherwise(
            { redirectTo: '/'}
        );
});

I get the first page when running the app but when I click on the links users and roles, nothing happens. 运行该应用程序时出现第一页,但是单击链接用户和角色时,没有任何反应。

UPDATED: the index.html is: 更新:index.html是:

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Spring boot and Angularjs Tutorial</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/css/app.css">
</head>
<body>
<h2>Administrator Panel</h2>
<div class="home-section">
    <ul class="menu-list">
        <li><a href="#/users">Users</a></li>
        <li><a href="#/roles">Roles</a></li>
    </ul>
</div>
<div ng-view></div>
<script src="/lib/angular.js"></script>
<script src="/lib/angular-resource.js"></script>
<script src="/lib/angular-route.js"></script>

<script src="/js/app.js"></script>
<script src="/js/controller.js"></script>



<link href="/css/bootstrap.css" rel="stylesheet" />
</body>
</html>

How can I fix this issue. 我该如何解决此问题。

[1]: [1]:

For link in AngularJS you should use ng-href directive: https://docs.angularjs.org/api/ng/directive/ngHref 对于AngularJS中的链接,您应该使用ng-href指令: https : //docs.angularjs.org/api/ng/directive/ngHref

Ex. 防爆。

<a ng-href="#/users">Users</a>
<ul class="menu-list">
    <li><a ui-sref="users">Users</a></li>
    <li><a ui-sref="roles">Roles</a></li>
</ul>

Try this one. 试试这个。 You can use ui-sref for navigation from html page 您可以使用ui-sref从html页面导航

您是否在模板开头声明了ng-app =“ app”

Here is how i fixed this issue: 这是我解决此问题的方法:

<div class="list-group">
                        <a href="#home" class="list-group-item"
                            ng-class="{active: currentNavLink == '/home'}"> <i
                            class="fa fa-home fa-lg"></i>&nbsp;Home
                        </a> <a href="#contacts" class="list-group-item"
                            ng-class="{active: currentNavLink == '/contacts'}"> <i
                            class="fa fa-user fa-lg"></i>&nbsp;Contacts
                        </a> <a href="#todos" class="list-group-item"
                            ng-class="{active: currentNavLink == '/todos'}"> <i
                            class="fa fa-indent fa-lg"></i>&nbsp;ToDos
                        </a>


                    </div>

where in the app.js: 在app.js中:

myApp.config(function($stateProvider, $urlRouterProvider) {

    $urlRouterProvider.when('', '/home');
    $urlRouterProvider.otherwise('/home');
    /*
    $urlRouterProvider.rule(function($injector, $location) {
      return '/home';
    });
    */
    $stateProvider
    .state('home', {
        url: '/home',
        templateUrl: 'templates/home.html',
        controller: 'HomeController'
    })
    .state('contacts', {
        url: '/contacts',
        templateUrl: 'templates/contacts.html',
        controller: 'ContactController'
    })  
    .state('todos', {
        url: '/todos',
        templateUrl: 'templates/todos.html',
        controller: 'TodoController'
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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