On my current webpage, the code looks like this:
<!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) Whenever I visit the url
http://localhost:7000/service/1#/test1
,
the browser makes two requests to the server - one to http://localhost:7000/service/1
and another to http://localhost:7000/archivarius/users/1
. Is there a way to
handle the first unnecessary request using an AngularJS controller? I want only the actions in
the test2 controller to occur when the user enters the url http://localhost:7000/service/1#/test1
. Is this possible?
2) In the routing configuration, why am I required to specify either a template or templateUrl? Why can't I just specify a controller for each route instead?