简体   繁体   English

角线(教程)不起作用

[英]Angular Routes (tutorial) not working

I am following google's angular tutorial log (different names for stuff I need), and I cannot get the routing to work correctly for the life of me. 我正在跟踪google的有角度的教程日志(我需要的东西使用不同的名称),并且我无法在我的一生中正常工作。 If I take the # out of the hrefs, i'll get a "Cannot GET" error". With the #, the URL changes but nothing happens. 如果我从hrefs中删除#号,则会收到“无法获取”错误。使用#号,URL会更改,但什么也不会发生。

controllers.js controllers.js

var AppControllers = angular.module('AppControllers',[]);

AppControllers.controller('homeCtrl', ['$scope', '$http',
    function($scope, $http){
  $http.get('javascripts/apprentices.json').success(function(data){
        $scope.apprentices = data;

  });
  $scope.orderProp = 'semester';
}]);

AppControllers.controller('apprenticeCtrl',['$scope', '$routeParams',
    function($scope, $routeParams) {
console.dir('hi');
      $http.get('javascripts/person.json').success(function(data) {
        // $scope.apprentices = data;
      });
    }]);

app.js app.js

var App = angular.module('App',[
  'ngRoute',
  'AppControllers']);

App.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
    when('/apprentices', {
        templateUrl: 'views/apprentice.html',
        controller: 'apprenticeCtrl'
      }).
      otherwise({
        templateUrl:'../views/home/index.html',
                redirectTo: '/asfsfsf'
      });
  }]);

index.html 的index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <title></title>
    <link rel="stylesheet" href="/stylesheets/style.css">
    <script src="javascripts/angular/angular.js"></script>
    <script src="javascripts/angular/angular-route.js"></script>
    <script src="javascripts/app.js"></script>
    <script src="javascripts/controllers.js"></script>
    <body>
      <div ng-app="App">
        <div ng-controller="homeCtrl">
          code
      </div>
      <div ng-view></div>
    </body>
  </head>
</html>

Looks like you just need to move the ng-app tag up to the body element. 看起来您只需要将ng-app标签移到body元素上即可。 The scope of the app is defined in the ng-app element and not it's siblings: 应用程序的范围是在ng-app元素中定义的,而不是它的同级元素:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title></title>
    <link rel="stylesheet" href="/stylesheets/style.css">
    <script src="javascripts/angular/angular.js"></script>
    <script src="javascripts/angular/angular-route.js"></script>
    <script src="javascripts/app.js"></script>
    <script src="javascripts/controllers.js"></script>
    <body ng-app="App">
      <div>
        <div ng-controller="homeCtrl">
          code
      </div>
      <div ng-view></div>
    </body>
  </head>
</html>

Another option would be to put the ng-view div inside the ng-app div if so desired. 如果需要的话,另一个选择是将ng-view div放在ng-app div中。

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

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