angular模板加载

angular模板加载 ----ng-template

Angularjs作为mvc(或者说mvvm)框架,同样具备模板这一基本概念。

NG加载模板的顺序为 内存加载---AJAX加载。

如果排版乱掉,请查阅 https://www.zybuluo.com/bornkiller/note/6023

内存加载

如果之前使用过Bootstrap 插件的ng版,即angular-ui,就会了解到这种方式的具体应用。模板本质上是字符串,把字符串直接写入内存,加载时直接从内存获取,速度会更快,有两种方式显式启用内存加载。

  • 通过使用 $templateCache service来实现
angular.module('myApp', [])
  .controller('myCtrl', ['$scope','$templateCache', function($scope,$templateCache){
       var tmp = '<h4>lovestoryh4>'
             + '<p>这是直接调用$templateCache服务获取模板文件的方式p>'
             + '<a href="http://www.baidu.com">服务启用templateCache方式a>';
       $templateCache.put('lovestory.html',tmp);                
   }])

$templateCache 服务put方法负责向内存写入模板内容。

  • 通过 script 标签引入

                    
                    

你可能感兴趣的:(javascript,angular)