简体   繁体   中英

Configure routes angular for menu

Confused with routes configuration in angular js. I want to learn how to configure this and don't import all the dependencies for each html file.

This is what I do not want to have foreach file:

  <script src="../../resources/jquery/jquery.min.js"></script>
<link rel="stylesheet" href="../../resources/bootstrap/css/bootstrap.min.css" />
<link rel="stylesheet" href="../../resources/toastr/toastr.min.css" />
<script src="../../resources/angular/angular.min.js"></script>
<script src="../../resources/bootstrap/js/bootstrap.min.js"></script>
<script src="../../resources/toastr/toastr.min.js"></script>
<script src="../services/book.service.js"></script>
<script src="controllers/book.controller.js"></script>

1 - My appConfig.js with routes:

var app = angular.module("library.crud", ["ngRoute"]);

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

    $routeProvider
        .when('/', {
            templateUrl: 'app/views/home.html',
            controller: 'homeController',
            controllerAs: 'vm'
        })
        .when('/book', {
              templateUrl: 'app/views/book.html',
              controller: 'bookController',
              controllerAs: 'vm'
        })
        .when('/books', {
            templateUrl: 'app/views/books.html',
            controller: 'booksController',
            controllerAs: 'vm'
        })
    .otherwise({
        redirectTo: '/'
    })


    $locationProvider.html5Mode({
        enabled: true,
        requireBase: false
    });
}]);

How do I use the appConfig file ?

My index menu:

<html ng-app="library.crud">
  <script src="appConfig.js"> </script>

<body ng-app="library.crud">
  <a href="#/books">books</a>
  <a href="#/book">book</a>

  <div ng-view></div>
</body>
</html>

My index file is not working and show the message: "injector moduler etc..."

This was supposed to be a comment, but I'd need 50 points of reputation.

You need to add the angular-route.js file to your page.

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script>

Be careful with the AngularJS version you are using, the above is just an example.

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