angularjs指令:replace与transclude的区别

2023-12-23 05:58

本文主要是介绍angularjs指令:replace与transclude的区别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


将视图模板(Template或TemplateUrl)替换到指定位置的视图(Restrict),

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>

显示结果:

Markdown

分析:

审查元素:

对比replace为true、false的区别:
Markdown

对比transclude为true、false的区别:
Markdown

具体审查:

<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的区别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/526959

相关文章

JAVA覆盖和重写的区别及说明

《JAVA覆盖和重写的区别及说明》非静态方法的覆盖即重写,具有多态性;静态方法无法被覆盖,但可被重写(仅通过类名调用),二者区别在于绑定时机与引用类型关联性... 目录Java覆盖和重写的区别经常听到两种话认真读完上面两份代码JAVA覆盖和重写的区别经常听到两种话1.覆盖=重写。2.静态方法可andro

C++中全局变量和局部变量的区别

《C++中全局变量和局部变量的区别》本文主要介绍了C++中全局变量和局部变量的区别,全局变量和局部变量在作用域和生命周期上有显著的区别,下面就来介绍一下,感兴趣的可以了解一下... 目录一、全局变量定义生命周期存储位置代码示例输出二、局部变量定义生命周期存储位置代码示例输出三、全局变量和局部变量的区别作用域

MyBatis中$与#的区别解析

《MyBatis中$与#的区别解析》文章浏览阅读314次,点赞4次,收藏6次。MyBatis使用#{}作为参数占位符时,会创建预处理语句(PreparedStatement),并将参数值作为预处理语句... 目录一、介绍二、sql注入风险实例一、介绍#(井号):MyBATis使用#{}作为参数占位符时,会

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Javaee多线程之进程和线程之间的区别和联系(最新整理)

《Javaee多线程之进程和线程之间的区别和联系(最新整理)》进程是资源分配单位,线程是调度执行单位,共享资源更高效,创建线程五种方式:继承Thread、Runnable接口、匿名类、lambda,r... 目录进程和线程进程线程进程和线程的区别创建线程的五种写法继承Thread,重写run实现Runnab

C++中NULL与nullptr的区别小结

《C++中NULL与nullptr的区别小结》本文介绍了C++编程中NULL与nullptr的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编... 目录C++98空值——NULLC++11空值——nullptr区别对比示例 C++98空值——NUL

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Go语言中make和new的区别及说明

《Go语言中make和new的区别及说明》:本文主要介绍Go语言中make和new的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 概述2 new 函数2.1 功能2.2 语法2.3 初始化案例3 make 函数3.1 功能3.2 语法3.3 初始化

深度解析Spring Boot拦截器Interceptor与过滤器Filter的区别与实战指南

《深度解析SpringBoot拦截器Interceptor与过滤器Filter的区别与实战指南》本文深度解析SpringBoot中拦截器与过滤器的区别,涵盖执行顺序、依赖关系、异常处理等核心差异,并... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现

Before和BeforeClass的区别及说明

《Before和BeforeClass的区别及说明》:本文主要介绍Before和BeforeClass的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Before和BeforeClass的区别一个简单的例子当运行这个测试类时总结Before和Befor