天天看點

AngularJs 指令directive之transclude

transclude - 編譯元素的内容,使它能夠被directive所用。需要(在模版中)配合ngtransclude使用(引用)

先看例子,index.js代碼:

AngularJs 指令directive之transclude

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

appmodule.directive('hello', function() {  

    return {  

        restrict: 'e',  

        template: '<div>hi there <span ng-transclude></span></div>',  

        transclude: true  

    };  

});  

 html

AngularJs 指令directive之transclude

<html ng-app='app'>  

<head>  

    <meta http-equiv="content-type" content="text/html; charset=utf-8" />  

</head>  

<body>  

<hello>  

    <br/>  

    <span>原始的内容,</span><br/>  

    <span>還會在這裡。</span>  

</hello>  

</body>  

<script type="text/javascript" src="../vendor/angular/angular.js"></script>  

<script type="text/javascript" src="app/index.js"></script>  

</html>  

 運作結果

AngularJs 指令directive之transclude

 firebug再看頁面代碼變成了

AngularJs 指令directive之transclude

<div>hi there   

<span ng-transclude="">  

</span>  

</div>  

<hello <div="">hi there   

</hello>