加载角度视图

Gya*_*raw 6 javascript ajax angularjs angular-routing ng-view

每当我们.html以角度加载服务某些控制器的文件时.不angular使一个ajax调用以检索该html.

喜欢这段代码.

 .state('home', {
              url: '/',
              templateUrl: '/Modules/Signin/signin.html',
              controller: 'SigninCtrl'
          })
Run Code Online (Sandbox Code Playgroud)

我的意思是在提取signin.html时询问

  • ajax打电话了吗?
  • 或者它们是否作为普通资源加载?
  • 如果ajax打电话,我在哪里可以找到一些关于它的文件.

Sha*_*wal 2

当您的代码执行时,Angular 首先查找$templateCache带有id /Modules/Signin/signin.html.

如果在 then yes 中没有找到$templateCacheAngular 将执行一个AJAX调用$http来获取 HTML 内容,并将其作为普通资源加载,该资源应位于 URL 处,如下所示:

http://example.com/Modules/Signin/signin.html
Run Code Online (Sandbox Code Playgroud)

您可以在浏览器的开发人员控制台中验证 AJAX 调用是否已执行。

阅读有关$templateCache 的更多信息。

$templateCache基本上,每个模板在尚未存储在缓存中时都会存储在 中。index.html因此,如果您在您或任何位置(例如您的angular安装位置)定义以下内容:

<script type="text/ng-template" id="/Modules/Signin/signin.html">
  <p>This is the content of the template</p>
</script>
Run Code Online (Sandbox Code Playgroud)

$templateCache现在,作为您的 Angular 引导程序,您的id中将会有一个数据/Modules/Signin/signin.html,因此现在您的state代码将不会进行任何 AJAX,而是只会加载上面定义的内容。