Angularjs: how to avoid calling the route in the first page load

Asked

Viewed 207 times

2

I need to get the page fully loaded, without calling any routes, when the user accesses through the direct link. The problem occurs when the page loads, Angular simply reads the path of the URL and calls the route. Routes can only be called when I click on a button.

How to avoid calling the route after page loading?

  • How is the loading of your app and scripts in your html? You are using ng-app or by manually starting the app with angular.bootstrap()?

  • I’m still using ng-app. As I am developing the project, I will need to use angular.bootstrap to start the app manually. And the loading of the scripts is being on-demand. That is, the user loads only the app files. s and Routes.js for the app setup, and as it navigates the pages, other files will be uploaded along with your controllers.

  • Hm, how do you make this shipment on-demand? Uses Requirejs?

  • As your user browses it loads apps, routers, and controllers? If so, how are you doing? Which route component are you using?

  • I’m using the ocLazyLoader.

  • Damn, I didn’t know this ocLazyLoad, I won’t be able to help you in this issue but thank you for commenting on this component.

  • Oops! Happy to help! Take a look at my last question asked here at Stack Angularjs: how to work with file-separated controllers. It was there that a guy referred me to ocLazyLoader. But anyway, thank you. I will continue to pursue this issue.

Show 2 more comments

1 answer

0

After all my research, I was unsuccessful. But I decided to create my simple method that solved my problem and I know that can help anyone who has this same question.

Code:

angular.module("myApp", [])

.run(function($rootScope) {

   // privates
   var isPageLoaded = false;

   // constructs
   $rootScope.$on("$routeChangeStart", function(event, next, current) {
      if (!isPageLoaded) {
         isPageLoaded = true;
         event.preventDefault();
      }
   });
});

Now your page loading will be complete, without the need for a second request made by route. Just click on a button contenting the path of a route it will be triggered normally, just not when the page is loaded by the URL.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.