angularjs指令:replace与transclude的区别
2017-02-23 15:13
309 查看
将视图模板(Template或TemplateUrl)替换到指定位置的视图(Restrict),
replace:自定义指令名称是否保留。
true:不保留指令名
false:保留指令名(默认)
Transclude:是否将原来视图的内容嵌入到视图模板(Template或TemplateUrl)中。
true:保留替换前的节点内容。
false:直接覆盖原有内容。
ng-tranclude决定了在什么地方放置嵌入部分。
显示结果:
对比replace为true、false的区别:
对比transclude为true、false的区别:
具体审查:
replace:自定义指令名称是否保留。
true:不保留指令名
false:保留指令名(默认)
Transclude:是否将原来视图的内容嵌入到视图模板(Template或TemplateUrl)中。
true:保留替换前的节点内容。
false:直接覆盖原有内容。
ng-tranclude决定了在什么地方放置嵌入部分。
<!DOCTYPE html> <html ng-app="app"> <head> <meta charset="utf-8"> <title>My AngularJS App</title> <script src="http://sandbox.runjs.cn/uploads/rs/376/pbcx3e1z/angular.min.js"></script> <script src="http://sandbox.runjs.cn/uploads/rs/376/pbcx3e1z/angular-route.min.js"></script> </head> <body> <hello> <br/> <span>原始的内容,</span><br/> <span>还会在这里。</span> </hello> <hello></hello> <br> <hello1> <br/> <span>原始的内容1,</span><br/> <span>还会在这里1。</span> </hello1> <hello1></hello1> <br> <hello2> <br/> <span>原始的内容2,</span><br/> <span>还会在这里2。</span> </hello2> <hello2></hello2> <br> <hello3> <br/> <span>原始的内容3,</span><br/> <span>还会在这里3。</span> </hello3> <hello3></hello3> <br> <hello4> <br/> <span>原始的内容4,</span><br/> <span>还会在这里4。</span> </hello4> <hello4></hello4> <br> <hello5> <br/> <span>原始的内容5,</span><br/> <span>还会在这里5。</span> </hello5> <hello5></hello5> <script> var appModule = angular.module('app', []); appModule .directive('hello', function () { return { restrict: 'E', template: '<div>Hi there :transclude: true<span ng-transclude></span></div>', transclude: true /*把<hello>标签替换成我们所编写的HTML模板, 但是<hello>标签内部的内容保持不变。*/ }; }) .directive('hello1', function () { return { restrict: 'E', template: '<div>Hi there 1 :transclude: false<span ng-transclude></span></div>', transclude: false }; }) .directive('hello2', function () { return { restrict: 'E', template: '<div>Hi there 2 :replace: true</div>', replace: true/* 将视图模板template替换到指定位置hello2 */ }; }) .directive('hello3', function () { return { restrict: 'E', template: '<div>Hi there 3 : replace: false</div>', replace: false/*默认为 false*/ }; }) .directive('hello4', function () { return { restrict: 'E', /*ng-tranclude决定了在什么地方放置嵌入部分。*/ template: '<div>Hi there 4 :replace: true, transclude: true<span ng-transclude></span></div>', replace: true, /*默认为 false*/ transclude: true }; }) .directive('hello5', function () { return { restrict: 'E', template: '<div>Hi there 5 :replace: false, transclude: false<span ng-transclude></span></div>', replace: false, /*默认为 false*/ transclude: false }; }); </script> </body> </html>
显示结果:
分析:
审查元素:对比replace为true、false的区别:
对比transclude为true、false的区别:
具体审查:
<body> <hello><div>Hi there :transclude: true<span ng-transclude=""> <br class="ng-scope"> <span class="ng-scope">原始的内容,</span><br class="ng-scope"> <span class="ng-scope">还会在这里。</span> </span></div></hello> <hello><div>Hi there :transclude: true<span ng-transclude=""></span></div></hello> <br> <hello1><div>Hi there 1 :transclude: false<span ng-transclude=""></span></div></hello1> <hello1><div>Hi there 1 :transclude: false<span ng-transclude=""></span></div></hello1> <br> <div>Hi there 2 :replace: true</div> <div>Hi there 2 :replace: true</div> <br> <hello3><div>Hi there 3 : replace: false</div></hello3> <hello3><div>Hi there 3 : replace: false</div></hello3> <br> <div>Hi there 4 :replace: true, transclude: true<span ng-transclude=""> <br class="ng-scope"> <span class="ng-scope">原始的内容4,</span><br class="ng-scope"> <span class="ng-scope">还会在这里4。</span> </span></div> <div>Hi there 4 :replace: true, transclude: true<span ng-transclude=""></span></div> <br> <hello5><div>Hi there 5 :replace: false, transclude: false<span ng-transclude=""></span></div></hello5> <hello5><div>Hi there 5 :replace: false, transclude: false<span ng-transclude=""></span></div></hello5> <script> var appModule = angular.module('app', []); appModule .directive('hello', function () { return { restrict: 'E', template: '<div>Hi there :transclude: true<span ng-transclude></span></div>', transclude: true /*把<hello>标签替换成我们所编写的HTML模板,但是<hello>标签内部的内容保持不变。*/ }; }) .directive('hello1', function () { return { restrict: 'E', template: '<div>Hi there 1 :transclude: false<span ng-transclude></span></div>', transclude: false }; }) .directive('hello2', function () { return { restrict: 'E', template: '<div>Hi there 2 :replace: true</div>', replace: true/* 将视图模板template替换到指定位置hello2 */ }; }) .directive('hello3', function () { return { restrict: 'E', template: '<div>Hi there 3 : replace: false</div>', replace: false/*默认为 false*/ }; }) .directive('hello4', function () { return { restrict: 'E', template: '<div>Hi there 4 :replace: true, transclude: true<span ng-transclude></span></div>', replace: true, /*默认为 false*/ transclude: true }; }) .directive('hello5', function () { return { restrict: 'E', template: '<div>Hi there 5 :replace: false, transclude: false<span ng-transclude></span></div>', replace: false, /*默认为 false*/ transclude: false }; }); </script> </body>
相关文章推荐
- angularjs指令中的replace与transclude参数
- 详解angularjs指令中的replace与transclude参数
- angularjs指令中的replace与transclude参数
- angularjs指令系统系列课程(3):替换replace,内容保留transclude,作用方式restrict
- angularjs指令参数transclude
- angularJS——ng-bind指令与插值的区别
- angularjs 指令详解 - template, restrict, replace
- AngularJS展示数据的ng-bind指令和{{}} 区别
- AngularJS展示数据的ng-bind指令和{{}} 区别
- angularjs 指令详解 - template, restrict, replace
- AngularJS展示数据的ng-bind指令和{{}} 区别
- angularjs指令中scope参数 true、false、{} 的区别详解
- 创建自定义 AngularJS 指令:Part 4 transclude和restrict介绍
- angularjs 指令详解 - template, restrict, replace
- AngularJS的指令(Directive) compile和link的区别及使用示例
- AngularJS中给ng-app指令赋值和不赋值的区别
- AngularJS指令中compile与link的区别
- AngularJS展示数据的ng-bind指令和{{}} 区别
- angularjs 指令详解 - template, restrict, replace
- angularjs指令:transclude