angularjs - 将 ng-include 指向包含脚本指令的部分 [英] angularjs - Point ng-include to a partial that contains a script directive

查看:19
本文介绍了angularjs - 将 ng-include 指向包含脚本指令的部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对原始问题的

当使用 ng-include 指令在所有页面上重用我的标题 HTML 时,加载/渲染顶部导航栏会有轻微的延迟.为了缓解这个问题,我试图将 ng-include 指向一个包含 <script type='text/ng-template' id='header.html> 的 header.html 文件;...</script> 以便它预加载部分.

但我似乎无法让它工作.我只有在 index.html 中直接粘贴 header.html 的内容时才成功,这违背了重用 header 代码的目的.

script 的文档仅给出了使用内联模板的示例,和 ngInclude 的文档不使用 script在示例模板中.

ngInclude 可以加载使用 angular 的 script 指令的文件吗?

index.html:

<html lang="en" ng-app="myApp"><头><title>测试</title><link rel="stylesheet" href="css/bootstrap.css"><link rel="stylesheet" href="css/custom.css"><script src='lib/angular/angular.js'></script><script src='js/main.js'></script><身体><div ng-controller="HeaderCtrl"><div ng-include src="header.url"></div><!-- 脚本在直接放入 index.html 时工作:<script type="text/ng-template" id="header.html"><div class="navbar navbar-fixed-top"><div class="navbar-inner"><div class="容器"><ul class="nav"><li><a class="brand" href="#">Test</a><li><a href="#">一个</a></li><li><a href="#">两个</a></li><li><a href="#">三</a></li>

-->

</html>

header.html:

main.js:

"使用严格";//我也用use strict"试过这个;移除var myApp = angular.module('myApp', []);函数 HeaderCtrl($scope) {$scope.header = {name: "header.html", url: "header.html"};}

解决方案

</div>

应该

<div ng-include src="'header.url'"></div>

不确定这是否是 rjm226 所说的需要双引号"的意思.ng-include 需要 double 和 single.我最近被这个绊倒了.

This is an edit of the original question:

When using the ng-include directive to reuse my header HTML on all pages, there is a slight delay in loading/rendering the top navbar. To alleviate the problem, I'm attempting to point ng-include to a header.html file that contains <script type='text/ng-template' id='header.html>...</script> so that it pre-loads the partial.

But I can't seem to get it to work. I've only had success when sticking the contents of header.html directly in index.html, which defeats the purpose of reusing the header code.

The docs for script only give an example of using an inline template, and the docs for ngInclude don't use script in the example templates.

Can ngInclude load a file that uses angular's script directive?

index.html:

<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
  <title>Testing</title>
  <link rel="stylesheet" href="css/bootstrap.css">
  <link rel="stylesheet" href="css/custom.css">
  <script src='lib/angular/angular.js'></script>
  <script src='js/main.js'></script>
</head>
<body>

<div ng-controller="HeaderCtrl">
  <div ng-include src="header.url"></div>
  <!-- script works when it is put directly in index.html:
  <script type="text/ng-template" id="header.html">
  <div class="navbar navbar-fixed-top">
    <div class="navbar-inner">
      <div class="container">
        <ul class="nav">
          <li>
            <a class="brand" href="#">Test</a>
          </li>
          <li><a href="#">One</a></li>
          <li><a href="#">Two</a></li>
          <li><a href="#">Three</a></li>
        </ul>
      </div>
    </div>
  </div>
  </script>
  -->
</div>
</body>
</html>

header.html:

<script type="text/ng-template" id="header.html">
  <div class="navbar navbar-fixed-top">
    <div class="navbar-inner">
      <div class="container">
        <ul class="nav">
          <li>
            <a class="brand" href="#">Test</a>
          </li>
          <li><a href="#">One</a></li>
          <li><a href="#">Two</a></li>
          <li><a href="#">Three</a></li>
        </ul>
      </div>
    </div>
  </div>
</script>

main.js:

"use strict";
// I've also tried this with "use strict"; removed

var myApp = angular.module('myApp', []);

function HeaderCtrl($scope) {
    $scope.header = {name: "header.html", url: "header.html"};
}

解决方案

<div ng-include src="header.url"></div>

should be

<div ng-include src="'header.url'"></div>

Not sure if this is what rjm226 meant by 'double quotes were necessary'. Both double and single are necessary for ng-include. I've been tripped up by this recently.

这篇关于angularjs - 将 ng-include 指向包含脚本指令的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆