zoukankan      html  css  js  c++  java
  • --@angularJS--模板加载之缓存模板demo

    不用缓存模板的写法如下:

    1、index.html:

    <!DOCTYPE HTML>
    <html ng-app="app">
    <head>
        <title>directive-templateUrl</title>
        <meta charset="utf-8">    
        <link rel="stylesheet" href="../css/bootstrap.css">
        <script src="../js/angular.js"></script>
    </head>
    <body>
    <!-- 下面是加载指令模板的总入口文件. -->
    <div>
    <!-- hello标签就代表了templateUrl中加载的模板碎片代码.注意:这样直接加载ff是没什么问题,chrome因为同源安全策略直接就不显示,解决办法是:用缓存模板(对象$templateCache) -->
        <hello></hello>
    </div>
    </body>
    <script src="hello.js"></script>
    </html>

    2、hello.js:

    var myModule = angular.module("app",[]);
    myModule.directive('hello',function(){
        return {
            restrict:'AE',
            replace:true,
            templateUrl:'helloTemplate.html'//helloTemplate.html文件表示的就是<div>Hi there</div>这一串html模板代码碎片文件,hello标签就代表这个模板
        }
    });

    3、模板文件——helloTemplate.html:

    <div>Hi there</div>    

    [解析]:

    加载效果:ff页面显示Hi there.chrome页面空白

    上面的注意部分已经说过,因为同源安全策略chrome并不显示模板文件,解决办法就是用缓存模板对象$templateCache来加载,那么我们下面来改造下js文件即可:

    2、hello.js:

    var myModule = angular.module("app",[]);
    myModule.run(function($templateCache){
        $templateCache.put("helloTemplate.html","<div>hello everyone.</div>");
    });// 向缓存中存入模板文件,并用put()方法向模板文件写入<div>hello everyone.</div>这段代码.注意这段代码会覆盖模板文件原有的代码碎片
    myModule.directive('hello',function($templateCache){
        return {
            restrict:'AE',
            replace:true,
            template:$templateCache.get("helloTemplate.html")// 用get()方法从缓存模板对象获取模板文件
        }
    });

    [解析]:

    加载效果:ff和chrome:页面均显示hello everyone.(原代码<div>Hi there</div>被覆盖)

  • 相关阅读:
    ThinkPHP5.0被攻击,发现漏洞
    ThinkPHP5.0引用PHPExcel插件,在页面中导出数据库数据
    ThinkPHP5.0引入插件
    引入UEditor插件
    Form
    点击链接只跳转到首页/本地正常,上传后,除首页外,其余页面404
    Thinkphp5中嵌套循环
    ARouter转场动画无效,试试下面这种写法
    windows下运行.sh文件
    List集合增删元素时,UnsupportedOperationException报错问题
  • 原文地址:https://www.cnblogs.com/koleyang/p/4517246.html
Copyright © 2011-2022 走看看