我正在关注谷歌的角度教程日志(我需要的东西的不同名称),我无法让路由在我的生活中正常工作。如果我从href中取出#,我将得到一个“无法获取”错误“。使用#,URL会发生变化,但没有任何反应。
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
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
<!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>
答案 0 :(得分:2)
看起来你只需要将ng-app标签移动到body元素。应用程序的范围在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>
如果需要,另一个选择是将ng-view div放在ng-app div中。