简体   繁体   English

加载角度视图

[英]Loading Angular View

Whenever we load .html files serving some controller in angular. 每当我们加载.html文件以某种角度服务于某些控制器时。 Does angular makes an ajax call to retrive that html . angular是否进行了ajax调用来检索该html

Like this piecec of code. 就像这段代码。

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

I mean to ask while fetching signin.html 我的意思是在获取signin.html时问

  • Is an ajax call made? 是否进行了ajax调用?
  • Or are they loaded as normal resources? 还是将它们作为普通资源加载?
  • If an ajax call is made where can i find some documentation written about it. 如果进行了ajax调用,我在哪里可以找到有关它的书面文档。

When your that code executes, Angular first lookup the HTML template in $templateCache with the id /Modules/Signin/signin.html . 当您执行该代码时,Angular首先在$templateCache查找id /Modules/Signin/signin.html的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: 如果在$templateCache找不到它,那么Angular将使用$http进行AJAX调用以获取HTML内容并将其作为常规资源加载,资源应位于URL处,例如:

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

You can verify it in your browser's developer's console that an AJAX call was performed. 您可以在浏览器的开发人员控制台中验证是否已执行AJAX调用。

Read more about $templateCache . 了解有关$ templateCache的更多信息。

Basically, every template get's stored in the $templateCache when it is not stored already in the cache. 基本上,每个模板获取都存储在$templateCache ,而尚未存储在缓存中。 So if you define the following in your index.html or any place (like where your angular is installed): 因此,如果您在index.html或任何位置(例如安装angular位置)定义以下内容:

<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. 现在,作为您的Angular引导程序,您的$templateCache中将有一个ID为/Modules/Signin/signin.html的数据,因此现在您的state代码将不会生成任何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 您在inspect元素的network标签中,重新加载时可以看到每个html部件均单独加载

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. 至少在ui-router中(假定视图不在templateCache中),将使用GET检索HTML文件,而不是使用对端点的AJAX调用来检索HTML文件。 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. 在您的情况下,它是<your root URL>/Modules/Signin/signin.html您可以在浏览器的开发工具中看到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM