简体   繁体   English

AngularJS路由功能不起作用

[英]AngularJS Routing Function is not working

My Angular Routing Function is not working - There is a page load, but without the 'home.html' file. 我的角路由功能不起作用-有页面加载,但是没有'home.html'文件。 This is my code: 这是我的代码:

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");
}

The route configuration is done in config and not controller. 路由配置是在config中而不是在控制器中完成的。 Change your code as below: 如下更改代码:

(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");
  }
});

Angular $providers working just in config state. Angular $ providers仅在配置状态下工作。 Eg: 例如:

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

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

};

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

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