When using routing with AngularJS, the script will send extra HTTP requests to load a view. However if the user is using a direct link to that route then how can we avoid the extra requests being made? I.e. load everything from the server using a single request instead of a second request for the view?
Edit:
I need to make clear what I mean.
If a user goes to website.com#/second where second is a route with a corresponding view, then the routing system in AngularJS will load that view using a HTTP request. However I do not want this HTTP request to take place if the user has not visited any prior page in that website. Rather what should happen is that the server includes that view within the template. I.e. there wont be a need for the view to be fetched because the server has already included it.
In other words how can I stop the routing system? I can use an if statement, but where do I put it?
As I write this I assume a good place would be in the route handling function.
sampleApp.config(['$routeProvider',
function($routeProvider) {
var firstVisit = true;
if(!firstVisit){
$routeProvider.
when('/', {
templateUrl: 'sections/main.php',
controller: 'main_controller'
}).
when('/second', {
templateUrl: 'sections/second.php',
controller: 'second_controller'
}).
otherwise({
redirectTo: '/'
});
}
firstVisit = false;
}]);
Edit 2:
It did not work. I want the routing function to depend on the value of a variable.