Angularjs路由参数不工作
我正在我的Apache服务器中尝试以下代码,但它不工作(当单击链接时,什么也没有发生)。它应该显示每个链接的标题/内容 index.htmlAngularjs路由参数不工作,angularjs,parameter-passing,ngroute,ng-view,angularjs-ng-route,Angularjs,Parameter Passing,Ngroute,Ng View,Angularjs Ng Route,我正在我的Apache服务器中尝试以下代码,但它不工作(当单击链接时,什么也没有发生)。它应该显示每个链接的标题/内容 index.html <!doctype html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js"></script> <script
<!doctype html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular-route.js"></script>
<script src="app.js"></script>
<script src="post.js"></script>
</head>
<body ng-app="blogApp">
<nav>
<a href="#/post/1">Blog post</a>
<a href="#/post/2">Other post</a>
<a href="#/post/3">Last post</a>
</nav>
<!-- Place where our user will be displayed -->
<div ng-view>
</div>
</body>
</html>
感谢您的帮助。您的代码似乎很好,只是您错过了一个重要的部分
app.controller('postCtrl', postCtrl);
请看下面的演示
var-app=angular.module('blogApp',['ngRoute']);
//设置应用程序的配置
app.config(函数($routeProvider){
$routeProvider.when('/post/:posted'{
控制员:邮政编码,
templateUrl:'post.html'
})
})
//忽略下面的代码。这通常在单独的html文件中
.run(函数($templateCache){
$templateCache.put('post.html','post编号:{{{postId}}{{title}}!{{content}');
});
应用控制器(“后TRL”,后TRL);
功能postCtrl($scope,$routeParams){
$scope.title=“错误”;
$scope.content=“没有该编号的帖子”;
$scope.postId=$routeParams.postId;
交换机($routeParams.postId){
案例“1”:
$scope.title=“你好世界”;
$scope.content=“这是我的第一篇帖子”;
打破
案例“2”:
$scope.title=“职位编号2”;
$scope.content=“这是我的第一篇帖子”;
打破
案例“3”:
$scope.title=“最后一篇文章”;
$scope.content=“再见”;
打破
}
}
function postCtrl ($scope, $routeParams) {
$scope.title = "Error";
$scope.content = "No post with that number";
$scope.postId = $routeParams.postId;
switch ($routeParams.postId) {
case '1':
$scope.title = "Hello world";
$scope.content = "This is my first post";
break;
case '2':
$scope.title = "Post no 2";
$scope.content = "This is my first post";
break;
case '3':
$scope.title = "Last post";
$scope.content = "Bye";
break;
}
}
app.controller('postCtrl', postCtrl);