AngularJs 中的transclude的理解


Transclude是一個配置, 為了告訴AngularJs去獲取當前指令模版內部的所有內容(實際使用ng-transclude), 更多關於怎么創建一個包含其他元素的指令: documentation of directives

下面自定義一個指令用ng-transclude在指令模版中去指定你想插入的內容:

angular.module('app', []) .directive('hero', function () { return { restrict: 'E', transclude: true, scope: { name:'@' }, template: '<div>' + '<div>{{name}}</div><br>' + '<div ng-transclude></div>' + '</div>' }; });

代碼使用如下:

<hero name="superman">Stuff inside the custom directive</hero>

頁面顯示如下:

Superman

Stuff inside the custom directive

完整的例子: 

Index.html

<body ng-app="myApp"> <div class="AAA"> <hero name="superman">Stuff inside the custom directive</hero> </div> </body>

jscript.js

angular.module('myApp', []).directive('hero', function () { return { restrict: 'E', transclude: true, scope: { name:'@' }, template: '<div>' + '<div>{{name}}</div><br>' + '<div ng-transclude></div>' + '</div>' }; });

頁面: 

enter image description here

實現: 

enter image description here


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM