AngularJS路由功能不起作用

时间:2022-11-25 19:42:44

My Angular Routing Function is not working - There is a page load, but without the 'home.html' file. This is my code:

我的角度路由功能不起作用 - 有一个页面加载,但没有'home.html'文件。这是我的代码:

Index.html

的index.html

<html  ng-app="App" class="no-js" lang="en" >
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.min.js"></script>
    <script src="app.js"></script>    
  </head>

  <body ng-cloak> 
  <div ng-controller="main">
      <div ng-view></div>
    </div>
  </body>
</html>

app.js

app.js

(function () {
'use strict';

angular
    .module('App', ['ngRoute']) 
    .controller('$routeProvider', router)
    .controller('main', main);

function router($routeProvider) {

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

};
function main ($scope) {
console.log("done");
}

2 个解决方案

#1


3  

Angular $providers working just in config state. Eg:

Angular $提供程序仅在配置状态下工作。例如:

angular
.module('App', ['ngRoute']) 
.config(['$routeProvider', router]);
function router($routeProvider) {

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

};

#2


3  

The route configuration is done in config and not controller. Change your code as below:

路由配置在config而不是控制器中完成。更改您的代码如下:

(function () {
  'use strict';

   angular
    .module('App', ['ngRoute']) 
    .config(router)
    .controller('main', main);

  function router($routeProvider) {

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

  };

  function main ($scope) {
    console.log("done");
  }
});

#1


3  

Angular $providers working just in config state. Eg:

Angular $提供程序仅在配置状态下工作。例如:

angular
.module('App', ['ngRoute']) 
.config(['$routeProvider', router]);
function router($routeProvider) {

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

};

#2


3  

The route configuration is done in config and not controller. Change your code as below:

路由配置在config而不是控制器中完成。更改您的代码如下:

(function () {
  'use strict';

   angular
    .module('App', ['ngRoute']) 
    .config(router)
    .controller('main', main);

  function router($routeProvider) {

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

  };

  function main ($scope) {
    console.log("done");
  }
});