AngularJS routing with templateUrl

2019-04-06 14:19发布

I have a problem with the AngularJS routing: I don't get any feedback of the page. No errors or view-switches.

I checked my implementation of the module, but it's declared in the right way. Then I searched for typos such as templateURL, but I didn't find any. I also tried to use ng-href instead of href in the list, but then the links weren't clickable anymore.

Here is my plunker.

And my code:

  1. Created my navigation:

    <body ng-app="Productportfolio">
    
    <ul>
      <li>
        <a href="#/home">Home</a>
      </li>
      <li>
        <a href='#/privat'>Log in</a>
     </li>
    </ul>
    
    <ng-view></ng-view>
    
    <!--own js -->
    <script src="app.js"></script>
    <!--Controller -->
    <script src="ProductCtrl.js"></script>
    <!--Services -->
    <!--Direktives-->
    </body>
    
  1. Made the templates:

    //home.html
    <div>
      <h1> Home </h1>
    </div>
    
    //private.html
    <div>
      <h1> Private</h1>
    </div>
    
  2. Declared a Angular module:

    angular.module('Productportfolio', ['ngRoute'])
    
  3. Added the $routeProvider to my config:

    angular.module('Productportfolio', ['ngRoute', 'ProductService', 'ProductCtrl'])
    
    .config(['$routeProvider, $locationProvider', function ($routeProvider, $locationProvider) {
    
      $routeProvider
    
        .when('/home', {
            templateUrl: 'home.html',
            controller: 'ProductCtrl'
        })
    
        .when('/private', {
            templateUrl: 'private.html',
            controller: 'ProductCtrl'
        })
    
        .otherwise({
            redirectTo: '/home',
            controller: 'ProductCtrl'
        });
    
      $locationProvider.hashPrefix('!');
    
    }]);
    

3条回答
看我几分像从前
2楼-- · 2019-04-06 14:50

In your Plunker, there were some issues related to imports. To make it easy, I simply removed both jQuery and Bootstrap. I also put all your modules in a single app.js file.

There were some errors in your html:

<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>

    <!--AngularJS-->
    <script data-require="angularjs@1.5.8" data-semver="1.5.8" src="https://opensource.keycdn.com/angularjs/1.5.8/angular.min.js"></script>
    <script data-require="angular-route@1.5.8" data-semver="1.5.8" src="https://code.angularjs.org/1.5.8/angular-route.js"></script>
  </head>

  <body ng-app="Productportfolio">

  <ul>
    <li>
      <a ng-href="#home">Home</a>
    </li>
    <li>
      <a ng-href="#private">Private</a>
    </li>
  </ul>

    <ng-view></ng-view>

    <!--own js -->
    <script src="app.js"></script>
  </body>

</html>
  • Import Angular BEFORE ngRoute or any other module
  • Use ng-href='#routeName' or, otherwise

And in your js:

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

myApp.controller('ProductCtrl', ['$scope',function ($scope) {
  var vm = this;
}]);

myApp.config(['$routeProvider', function ($routeProvider) {

        $routeProvider
            .when('/home', {
                templateUrl: 'home.html',
                controller: 'ProductCtrl'
            })

            .when('/private', {
                templateUrl: 'private.html',
                controller: 'ProductCtrl'
            })

            .otherwise({
                redirectTo: '/home',
                controller: 'ProductCtrl'
            });
}]);
  • You need to inject only external modules in your app module, not all controller, services etc

Notice that, to make it easy, I removed also your Service since you did not share it and it was useful.

Last but not least, if you want to use $locationProvider.hashPrefix('!'); to use hashbangs, you need to add <base href="/" /> at the end of your head.

For some reason, plunker does not allow me to do that, but I'm sure you'll get it to work on your version.

Here You can find the working plunker reproducing your application.

Hope I've been helpful.

查看更多
The star\"
3楼-- · 2019-04-06 15:07

The remaining and not visible problem is here :

angular.module('Productportfolio', ['ngRoute', 'ProductService', 'ProductCtrl'])
.config(['$routeProvider, $locationProvider', 

config() function of module object takes as parameter, a array of strings and not a string with "," as separator char inside it. See example and doc here : https://docs.angularjs.org/guide/providers#provider-recipe

So, it should be :

angular.module('Productportfolio', ['ngRoute', 'ProductService', 'ProductCtrl'])
.config(['$routeProvider', '$locationProvider', 

Update :

But in fact, in your case, it works even without precising the array :

angular.module('Productportfolio', ['ngRoute', 'ProductService', 'ProductCtrl'])
.config(

I updated the plunker and the app.js with both versions. I have the impression that the array is not mandatory (any longer). So, it seems better to ignore that parameter, especially, if with bad value, it may create side-effects.

Here the plunker with corrections (ordered lib, typos and config function called) : http://plnkr.co/edit/NTn6Zmav5RX4V8zgHPOG?p=preview

I have removed $locationProvider.hashPrefix('!') as not suitable for the url you are using. See @AndreaM16 answer for that.

Besides, you have not declared your service you want to use in your controller.

app.js

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

        $routeProvider
            .when('/home', {
                templateUrl: 'home.html',
                controller: 'ProductCtrl'
            })

            .when('/private', {
                templateUrl: 'private.html',
                controller: 'ProductCtrl'
            })

            .otherwise({
                redirectTo: '/home',
                controller: 'ProductCtrl'
            });


        }]
    );

or app.js without the array parameter in config function :

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

        $routeProvider
            .when('/home', {
                templateUrl: 'home.html',
                controller: 'ProductCtrl'
            })

            .when('/private', {
                templateUrl: 'private.html',
                controller: 'ProductCtrl'
            })

            .otherwise({
                redirectTo: '/home',
                controller: 'ProductCtrl'
            });

       // $locationProvider.hashPrefix('!');
        }
    );

index.html :

<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <!--Bootstrap-->
    <script data-require="jquery@*" data-semver="3.0.0" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.js"></script>

    <script src="https://www.atlasestateagents.co.uk/javascript/tether.min.js"></script><!-- Tether for Bootstrap --> 
    <link data-require="bootstrap@*" data-semver="4.0.0-alpha.2" rel="stylesheet" href="https://cdn.rawgit.com/twbs/bootstrap/v4-dev/dist/css/bootstrap.css" />
    <script data-require="bootstrap@*" data-semver="4.0.0-alpha.2" src="https://cdn.rawgit.com/twbs/bootstrap/v4-dev/dist/js/bootstrap.js"></script>


    <!--AngularJS-->
        <script data-require="angularjs@1.5.8" data-semver="1.5.8" src="https://opensource.keycdn.com/angularjs/1.5.8/angular.min.js"></script>
    <script data-require="angular-route@*" data-semver="1.5.8" src="https://code.angularjs.org/1.5.8/angular-route.js"></script>


    <!--own js -->
    <script src="app.js"></script>
    <!--Controller -->
    <script src="ProductCtrl.js"></script>
    <!--Services -->
    <!--Direktives-->
  </head>

  <body ng-app="Productportfolio">

  <ul>
    <li>
      <a href="#home">Home</a>
    </li>
    <li>
      <a href="#private">Log in</a>
    </li>
  </ul>
  <div ng-view></div>


  </body>

</html>
查看更多
【Aperson】
4楼-- · 2019-04-06 15:08

The order of loading java script files is very important. Load in the following order:

<link data-require="bootstrap@*" data-semver="4.0.0-alpha.2" rel="stylesheet" 
              href="https://cdn.rawgit.com/twbs/bootstrap/v4-dev/dist/css/bootstrap.css" />
<script data-require="jquery@*" data-semver="3.0.0" 
              src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.js"></script>
<script src="https://www.atlasestateagents.co.uk/javascript/tether.min.js"></script>
<script data-require="bootstrap@*" data-semver="4.0.0-alpha.2" 
          src="https://cdn.rawgit.com/twbs/bootstrap/v4-dev/dist/js/bootstrap.js"></script>


<!--AngularJS-->
<script data-require="angularjs@1.5.8" data-semver="1.5.8" 
            src="https://opensource.keycdn.com/angularjs/1.5.8/angular.min.js"></script>
<script data-require="angular-route@*" data-semver="1.5.8" 
           src="https://code.angularjs.org/1.5.8/angular-route.js"></script>

It means that you are loading Bootstrap.js files, but Bootstrap cannot work without jQuery library. So it means you should load jQuery at first, then Bootstrap.js. And other libraries should be reordered(I've shown in an example above).

In addition, you can use Console in your browser to see whether there are errors and what errors you have.

查看更多
登录 后发表回答