我有以下页面:
<!doctype html>
<html lang="en" ng-app="myModule">
<head>
<meta charset="utf-8">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script>
var myModule = angular.module('somename', []);
myModule.config(function ($routeProvider) {
$routeProvider.
when('/zzz', {templateUrl:'' , controller: TestCtrl}).
when('/test1', {template:' ', controller: TestDataCtrl}).
when('/test2', {template:'/abc ', controller: function TestCtrl1() {alert("test2")} }).
when('/test/:userid', { controller: TestDataCtrl }).
when('/users/:userid', {templateUrl: '/users/:userid?html=true', controller: UserDataCtrl}).
otherwise({redirectTo: '/works'});
});
function TestCtrl($scope) { alert("test") }
function UserDataCtrl($scope, $http) {
...
}
function TestDataCtrl($scope, $http, $routeParams, $route) {
$http.get('users/1').success(function (data) {
console.log("UserDataCtrl");
$scope.user = data;
});
}
</script>
</head>
<body ng-app="myModule">
<div ng-view></div>
{{1+1}}
</body>
</html>
1)当我导航到以下网址http://localhost:7000/service/1#/test1
时,它总是向服务器(在我的情况下为 REST 服务)发送两个请求,例如http://localhost:7000/service/1
and http://localhost:7000/archivarius/users/1
。我可以以某种方式处理第一个(无用的)浏览器请求,也许使用 AngularJs 控制器?我的意思是当用户输入 url 时http://localhost:7000/service/1#/test1
,唯一的事情(请求)应该发生在 test2 控制器中。可能吗?
2)在路由配置中,为什么我必须指定模板或模板Url?为什么我不能只为路由指定控制器?