在绝大多数情况下,动态加载静态指令模板没有任何价值。它们是如此之小,以至于没有意义。但是,有可能。但是,大多数情况下,此策略用于动态模板。
它需要
$http获取模板并将
$compile其连接到AngularJS。
app.directive('testDirective', function($http,$compile) { return { scope: { show: '&' }, link: function( scope, element, attrs ) { var tpl, url = 'testDirective.tpl.html'; scope.$watch( 'show()', function (show) { if ( show ) { showTheDirective(); } }); function showTheDirective () { if ( !tpl ) { $http.get( url ).then( function ( response ) { tpl = $compile( response.data )( scope ); element.append(tpl); }); } } } };});这是一个表明它有效的Plunker。



