angular-day12-$transclude 发表于 2017-01-08 | 分类于 fe-framework , angular transclusiontransclude scope 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <script src="angular.js"></script></head><body ng-app='myapp'> <section ng-controller='ctrl'> <div msg2>this is body stuff</div> <div msg2>this is body stuff222</div> <div msg3>this is body stuff3333</div> <msg4 my-clude>this is body stuff44444444</msg4> </section></body><script type="text/javascript"> var module = angular.module('myapp', []); module.controller('ctrl', ['$scope', '$parse', '$interpolate', function ($scope, $parse, $interpolate) { $scope.a = 1; $scope.b = 2; $scope.change = function () { $scope.b = 30; }; }]); module.directive('msg', function () { return { templateUrl: 'trans1.html' } }); module.directive('msg2', function () { return { templateUrl: 'trans2.html', transclude: true } }); module.directive('msg3', function () { return { templateUrl: 'trans3.html', transclude: true, // link : function(scope, iElement, iAttrs, controller, transclude){ // var content = transclude(); // iElement.find("p").append(content); // } // 这也行 controller: function ($scope, $element, $attrs, $transclude) { var content = $transclude(); $element.find("section").append(content); } } }); module.directive('msg4', function () { return { templateUrl: 'trans4.html', transclude: true } }); module.directive('myClude', function () { return { link: function (scope, iElement, iAttrs, controller, transclude) { var content = transclude(); iElement.find("section").append(content); debugger } } }); module.run(function ($templateCache) { $templateCache.put('trans1.html', "<div>no transclusion</div>"); $templateCache.put('trans2.html', '<div>a +++ b ' + '<div ng-transclude >directive</div> </div>'); $templateCache.put('trans3.html', '<div>trans3.html ' + '<section id=pid>directive</section> </div>'); $templateCache.put('trans4.html', '<div>trans4.html ' + '<section id=pid></section> </div>'); });</script></html> transclude function demo code123456789101112131415161718192021222324252627282930313233343536373839404142434445<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <script src="angular.js"></script></head><body ng-app='myapp'> <div msg> esefffffffff</div></body><script type="text/javascript"> var module = angular.module('myapp', [ ]); module.controller('ctrl', function ($scope, $parse, $interpolate) { }); module.directive('msg', function () { return { templateUrl: 'templateId.html', transclude : true, controller: function ($scope, $element, $attrs,$transclude) { // var cont = $transclude(); // $element.find('span').append(cont); // $element.find('em').append(cont); // with call back $transclude(function(transEle){ $element.find('span').append(transEle); }); $transclude(function(transEle){ $element.find('em').append(transEle); }); } } }); module.run(function ($templateCache) { $templateCache.put('templateId.html', '<div><em style="color:#1f6"></em> ' +'<br><span style="color:#a56"></span> </div>'); });</script></html> transclusion scope transclusion scope with shared scope transclusion scope with inherited scope/isolated scope transclude scope / custom transclude scope12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <script src="angular.js"></script></head><body ng-app='myapp'> <div ng-controller="ctrl"> controller a{{a}} b{{b}} <div msg> transclude a{{a}} b{{$parent.b}}</div> <hr> <div msg2> transclude a{{a}} b{{b}}</div> <hr> <div msg3> transclude a{{a}} b{{b}}</div> </div></body><script type="text/javascript"> var module = angular.module('myapp', []); module.controller('ctrl', function ($scope, $parse, $interpolate) { $scope.a = 3; }); module.directive('msg', function () { return { templateUrl: 'isolate.html', transclude: true, scope: {}, controller: function ($scope, $element, $attrs, $transclude) { $scope.a = 1; $scope.b = 2 } } }); module.directive('msg2', function () { return { templateUrl: 'templateId.html', transclude: true, scope: {}, controller: function ($scope, $element, $attrs, $transclude) { $scope.a = 1; $scope.b = 2 $transclude(function (transEle, $transope) { $transope.a = 33; $transope.b = $scope.b; $element.find('span').append(transEle); }); } } }); module.directive('msg3', function () { return { templateUrl: 'templateId.html', transclude: true, scope: {}, controller: function ($scope, $element, $attrs, $transclude) { $scope.a = 1; $scope.b = 2 var custScope = $scope.$new(true);//create a isolate scope custScope.a = 66; custScope.b = 67; $transclude(custScope,function (transEle) { $element.find('span').append(transEle); }); } } }); module.run(function ($templateCache) { $templateCache.put('isolate.html', '<div>directive a{{a}} b{{b}} <br><i ng-transclude></i> </div>'); $templateCache.put('templateId.html', '<div><span style="color:#a56"></span> </div>'); });</script></html>