angular会进行Ajax调用来检索html吗?

每当我们加载.html文件以服务于angular中的某个控制器时,angular都会进行Ajax调用来检索该html吗?

就像这段代码:

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

会在获取signin.html时询问吗?

  • 1、发出Ajax调用了吗?
  • 2、或者它们是作为正常资源加载的?
  • 3、如果进行了Ajax调用,在哪里可以找到相关文档呢?

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

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.

In ui-router at least (assuming the view is not in the templateCache) view HTML files are retrieved with a GET to the URL of the file, rather than with an AJAX call to an endpoint. In your case, it'll be a GET to <your root URL>/Modules/Signin/signin.html - you can see this in your browser's development tools.