Template-expanding directive:

<div ng-controller="Controller">
<div my-customer></div>
</div> angular.module('docsSimpleDirective', [])
.controller('Controller', ['$scope', function($scope) {
$scope.customer = {
name: 'Naomi',
address: '1600 Amphitheatre'
};
}])
.directive('myCustomer', function() {
return {
template: 'Name: {{customer.name}} Address: {{customer.address}}'
};
}); 结果:Name: Naomi Address: Amphitheatre
<div ng-controller="Controller">
<div my-customer></div>
</div> angular.module('docsTemplateUrlDirective', [])
.controller('Controller', ['$scope', function($scope) {
$scope.customer = {
name: 'Naomi',
address: '1600 Amphitheatre'
};
}])
.directive('myCustomer', function() {
return {
templateUrl: 'my-customer.html'
};
}); my-customer.html: Name: {{customer.name}} Address: {{customer.address}} 结果:Name: Naomi Address: Amphitheatre

  

<div ng-controller="Controller">
<div my-customer type="name"></div>
<div my-customer type="address"></div>
</div> angular.module('docsTemplateUrlDirective', [])
.controller('Controller', ['$scope', function($scope) {
$scope.customer = {
name: 'Naomi',
address: '1600 Amphitheatre'
};
}])
.directive('myCustomer', function() {
return {
templateUrl: function(elem, attr){
return 'customer-'+attr.type+'.html';
}
};
}); customer-name.html:
Name: {{customer.name}} customer-address.html:
Address: {{customer.address}} 结果:Name: Naomi
Address: Amphitheatre

  

The restrict option is typically set to:

'A' - only matches attribute name
'E' - only matches element name
'C' - only matches class name
These restrictions can all be combined as needed: 'AEC' - matches either attribute or element or class name <div ng-controller="Controller">
<my-customer></my-customer>
</div> angular.module('docsRestrictDirective', [])
.controller('Controller', ['$scope', function($scope) {
$scope.customer = {
name: 'Naomi',
address: '1600 Amphitheatre'
};
}])
.directive('myCustomer', function() {
return {
restrict: 'E',
templateUrl: 'my-customer.html'
};
}); my-customer.html:
Name: {{customer.name}} Address: {{customer.address}} 结果:Name: Naomi Address: Amphitheatre

  

Isolating the Scope of a Directive:

<div ng-controller="NaomiController">
<my-customer></my-customer>
</div>
<hr>
<div ng-controller="IgorController">
<my-customer></my-customer>
</div> angular.module('docsScopeProblemExample', [])
.controller('NaomiController', ['$scope', function($scope) {
$scope.customer = {
name: 'Naomi',
address: '1600 Amphitheatre'
};
}])
.controller('IgorController', ['$scope', function($scope) {
$scope.customer = {
name: 'Igor',
address: '123 Somewhere'
};
}])
.directive('myCustomer', function() {
return {
restrict: 'E',
templateUrl: 'my-customer.html'
};
}); my-customer.html:
Name: {{customer.name}} Address: {{customer.address}}
结果:
Name: Naomi Address: 1600 Amphitheatre



Name: Igor Address: 123 Somewhere

<div ng-controller="Controller">
<my-customer info="naomi"></my-customer>
<hr>
<my-customer info="igor"></my-customer>
</div> angular.module('docsIsolateScopeDirective', [])
.controller('Controller', ['$scope', function($scope) {
$scope.naomi = { name: 'Naomi', address: '1600 Amphitheatre' };
$scope.igor = { name: 'Igor', address: '123 Somewhere' };
}])
.directive('myCustomer', function() {
return {
restrict: 'E',
scope: {
customerInfo: '=info'
},
templateUrl: 'my-customer-iso.html'
};
}); my-customer-iso.html:
Name: {{customerInfo.name}} Address: {{customerInfo.address}} 结果:Name: Naomi Address: Amphitheatre
Name: Igor Address: Somewhere
<div ng-controller="Controller">
<my-customer info="naomi"></my-customer>
</div> angular.module('docsIsolationExample', [])
.controller('Controller', ['$scope', function($scope) {
$scope.naomi = { name: 'Naomi', address: '1600 Amphitheatre' };
$scope.vojta = { name: 'Vojta', address: '3456 Somewhere Else' };
}])
.directive('myCustomer', function() {
return {
restrict: 'E',
scope: {
customerInfo: '=info'
},
templateUrl: 'my-customer-plus-vojta.html'
};
}); Name: {{customerInfo.name}} Address: {{customerInfo.address}}
<hr>
Name: {{vojta.name}} Address: {{vojta.address}} 结果:
Name: Naomi Address: Amphitheatre
Name: Address:

angular 自定义指令的更多相关文章

  1. Angular自定义指令(directive)

    angular自定义指令,意我们可以通过angula自己定义指令,来实现我们的特殊要求,为所欲为,一支穿云箭,千军万马来相见 多少年的老规矩了,先看代码: <!DOCTYPE html> ...

  2. angular 自定义指令详解 Directive

    在angular中,Directive,自定义指令的学习,可以更好的理解angular指令的原理,当angular的指令不能满足你的需求的时候,嘿嘿,你就可以来看看这篇文章,自定义自己的指令,可以满足 ...

  3. angular 自定义指令 directive transclude 理解

    项目中断断续续的用了下angular,也没狠下心 认真的学习.angular 特别是自定义指令这块 空白. transclude 定义是否将当前元素的内容转移到模板中.看解释有点抽象. 看解释有点抽象 ...

  4. Angular自定义指令directive:scope属性

    在AngularJS中,除了内置指令如ng-click等,我们还可以自定义指令.自定义指令,是为了扩展DOM元素的功能.代码中,通过指定directive中的restrict属性,来决定这个指令是作为 ...

  5. Angular17 Angular自定义指令

    1 什么是HTML HTML文档就是一个纯文本文件,该文件包含了HTML元素.CSS样式以及JavaScript代码:HTML元素是由标签呈现,浏览器会为每个标签创建带有属性的DOM对象,浏览器通过渲 ...

  6. angular自定义指令

    1.在directive文件下创建指令的js文件 通常自定义指令需要声明模块(注意定义指令时, js内部指令名称需采用 aaAaBb驼峰的命名方式  html中使用的是aa-aa-bb) e.g (f ...

  7. angular -- 自定义指令和模板

    angular 可以自定义一些指令,来简化我们前端的工作量. 第一种:简单指令示例: <h3>自定义指令</h3> <sheng></sheng> &l ...

  8. angular自定义指令命名的那个坑

    Directive 先从定义一个简单的指令开始. 定义一个指令本质上是在HTML中通过元素.属性.类或注释来添加功能.AngularJS的内置指令都是以ng开头,如果想自定义指令,建议自定义一个前缀代 ...

  9. angular自定义指令相关知识及代码

    原文地址 https://www.jianshu.com/p/0c015862156d 大纲 1.自定义指令之——属性指令 2.自定义属性指令的运行原理 3.自定义属性指令代码实践 4.自定义结构指令 ...

  10. angular自定义指令 repeat 循环结束事件;limitTo限制循环长度、限定开始位置

    1.获取repeat循环结束: 自定义指令: .directive('repeatFinish', function () { return { link: function (scope, elem ...

随机推荐

  1. Windows创建文件链接

    Windows平台创建文件.文件夹链接: 测试平台,windows10. D:\>mklink 创建符号链接. MKLINK [[/D] | [/H] | [/J]] Link Target / ...

  2. C# 中==与Equals方法比较

    先来段代码,如下: static void Main(string[] args) { string a = new string(new char[] { 'h', 'e', 'l', 'l', ' ...

  3. yum安装node.js

    1.安装EPEL库 yum install epel-release 2.安装Node.js yum install nodejs 3.安装nodejs中常用的npm软件包管理器 yum instal ...

  4. windows下安装和配置mongoDB

    上次在mac下安装和配置了mongodb,这次在windows下也尝试安装和配置mongodb. 1.首先下载mongodb压缩包,下载后解压到D盘或E盘.如下: 2.配置环境变量:桌面—计算机右键— ...

  5. NuGet的几个小技巧

    因为是转载文章 在此标明出处,以前有文章是转的没标明的请谅解,因为有些已经无法找到出处,或者与其它原因. 如有冒犯请联系本人,或删除,或标明出处. 因为好的文章,以前只想收藏,但连接有时候会失效,所以 ...

  6. BetterZip,支持rar等多种压缩解压方式(Xcode自身不能解压rar)

    百度云盘下载链接: http://pan.baidu.com/s/1sk7Faw9密码: muw7 rarosx-5.3.0.tar.gz1.解压之后放到/usr/local/目录下(位置可选,不过要 ...

  7. sqlserver 在脚本中,为所有字符前没有N标记的字符增加N

    {[^N]}{'[\u4e00-\u9fa5]|[\u4e00-\U9fa5]|[0-9]|[A-Z]} \1N\2

  8. Selenium2学习-025-WebUI自动化实战实例-023-页面快照截图应用之一 -- 常规截图(全页面)

    通常我们在进行自动化测试的过程中,有时候需要对页面进行截图,以保存此时的页面,用作后续的判断或测试报告.在 Web UI 自动化测试脚本过程中,通常有以下几种截图的要求: 常规截图 - 页面样式(全页 ...

  9. jq 拖拽

    1.尼玛, move事件的时候忘了加ev,找了一个多小时 <!DOCTYPE html> <html> <head lang="en"> < ...

  10. Power-BI For K3 免费版培训与交流!Q群视频培训,绝对干货!

    Power-BI  For K3 免费版培训与交流!Q群视频培训,绝对干货!1.产品安装与配置:2.产品使用:3.个性化开发(现场提需求现场开发):4.交流互动.时间:2015-12-03(周四)晚8 ...