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

相关文章

kotlin中const 和val的区别及使用场景分析

《kotlin中const和val的区别及使用场景分析》在Kotlin中,const和val都是用来声明常量的,但它们的使用场景和功能有所不同,下面给大家介绍kotlin中const和val的区别,... 目录kotlin中const 和val的区别1. val:2. const:二 代码示例1 Java

CSS Padding 和 Margin 区别全解析

《CSSPadding和Margin区别全解析》CSS中的padding和margin是两个非常基础且重要的属性,它们用于控制元素周围的空白区域,本文将详细介绍padding和... 目录css Padding 和 Margin 全解析1. Padding: 内边距2. Margin: 外边距3. Padd

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

分辨率三兄弟LPI、DPI 和 PPI有什么区别? 搞清分辨率的那些事儿

《分辨率三兄弟LPI、DPI和PPI有什么区别?搞清分辨率的那些事儿》分辨率这个东西,真的是让人又爱又恨,为了搞清楚它,我可是翻阅了不少资料,最后发现“小7的背包”的解释最让我茅塞顿开,于是,我... 在谈到分辨率时,我们经常会遇到三个相似的缩写:PPI、DPI 和 LPI。虽然它们看起来差不多,但实际应用

GORM中Model和Table的区别及使用

《GORM中Model和Table的区别及使用》Model和Table是两种与数据库表交互的核心方法,但它们的用途和行为存在著差异,本文主要介绍了GORM中Model和Table的区别及使用,具有一... 目录1. Model 的作用与特点1.1 核心用途1.2 行为特点1.3 示例China编程代码2. Tab

Nginx指令add_header和proxy_set_header的区别及说明

《Nginx指令add_header和proxy_set_header的区别及说明》:本文主要介绍Nginx指令add_header和proxy_set_header的区别及说明,具有很好的参考价... 目录Nginx指令add_header和proxy_set_header区别如何理解反向代理?proxy

Java中&和&&以及|和||的区别、应用场景和代码示例

《Java中&和&&以及|和||的区别、应用场景和代码示例》:本文主要介绍Java中的逻辑运算符&、&&、|和||的区别,包括它们在布尔和整数类型上的应用,文中通过代码介绍的非常详细,需要的朋友可... 目录前言1. & 和 &&代码示例2. | 和 ||代码示例3. 为什么要使用 & 和 | 而不是总是使

C++中函数模板与类模板的简单使用及区别介绍

《C++中函数模板与类模板的简单使用及区别介绍》这篇文章介绍了C++中的模板机制,包括函数模板和类模板的概念、语法和实际应用,函数模板通过类型参数实现泛型操作,而类模板允许创建可处理多种数据类型的类,... 目录一、函数模板定义语法真实示例二、类模板三、关键区别四、注意事项 ‌在C++中,模板是实现泛型编程

Spring中@RestController和@Controller的使用及区别

《Spring中@RestController和@Controller的使用及区别》:本文主要介绍Spring中@RestController和@Controller的使用及区别,具有很好的参考价... 目录Spring中@RestController和@Controller使用及区别1. 基本定义2. 使