Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading Angular View

Whenever we load .html files serving some controller in angular. Does angular makes an ajax call to retrive that html.

Like this piecec of code.

 .state('home', {
              url: '/',
              templateUrl: '/Modules/Signin/signin.html',
              controller: 'SigninCtrl'
          })

I mean to ask while fetching signin.html

  • Is an ajax call made?
  • Or are they loaded as normal resources?
  • If an ajax call is made where can i find some documentation written about it.
like image 389
Gyanesh Gouraw Avatar asked Dec 17 '15 10:12

Gyanesh Gouraw


2 Answers

When your that code executes, Angular first lookup the HTML template in $templateCache with the id /Modules/Signin/signin.html.

If it doesn't find that in the $templateCache then yes, Angular will do an AJAX call using $http to get the HTML content and it will be loaded as normal resource which should be located at the URL like:

http://example.com/Modules/Signin/signin.html

You can verify it in your browser's developer's console that an AJAX call was performed.

Read more about $templateCache.

Basically, every template get's stored in the $templateCache when it is not stored already in the cache. So if you define the following in your index.html or any place (like where your angular is installed):

<script type="text/ng-template" id="/Modules/Signin/signin.html">
  <p>This is the content of the template</p>
</script>

Now as your Angular bootstraps, there will be a data in your $templateCache with id /Modules/Signin/signin.html so now your state code will not make any AJAX instead it will simply load the content defined above.

like image 125
Shashank Agrawal Avatar answered Oct 28 '22 19:10

Shashank Agrawal


I think a call is made, when I look into my own project. You have in the inspect element the network tab, as you reload you can see that every html part is loaded separately

like image 30
Dennis Avatar answered Oct 28 '22 19:10

Dennis