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. 常用yum命令

    yum list 查询所有可用软件包 yum search 关键字 查询和关键字相关的包 yum -y install 包名 加上-y自动回答yes yum -y update 包名 升级  yum ...

  2. xcode5 和code6中push后方法执行的先后问题

    在xocde5中 执行的顺序是 prepareForSegue  .viewDidLoad. didSelectRowAtIndexPath,在xcode6中 执行的顺序是  prepareForSe ...

  3. 去除undefined和末尾逗号及把字符串数字转成数字数组的方法

    function removeundefined(str){   var v=new Array(),b="";   var tmp=fil(str); for(var i=0;i ...

  4. Partial Tree---hdu5534(完全背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题意:有n个节点,让这n个节点形成一棵树,这棵树的种类有很多种,现在告诉n-1个f[i],表示度 ...

  5. mongoDB 读书笔记(初级命令)

    关于mongoDB的相关知识,读书笔记,便于自己查阅用,不定期更新(纯手打)        <mongoDB权威指南> 一.创建更新和删除 1.创建 //批量插入一个集合可以节省时间,只用 ...

  6. leetcode算法

    Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...

  7. iOS:FFmpeg视频播放和直播框架

    视频直播和播放转码器框架 介绍: FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.采用LGPL或GPL许可证. 它提供了录制.转换以及流化音视频的完整解决方案.它 ...

  8. APICloud全面支持WiFi真机同步和实时预览功能

    APICloud工具插件包括APICloud Studio.Sublime Text和Webstorm全面为开发者提供iOS和Android平台真机同步调试功能,不仅可以通过USB方式进行APP真机同 ...

  9. Linux权限值问题

    0660:从左向右:第一位:(我不清楚,也没有用过)第二位:当前用户的经权限:6=110(二进制),每一位分别对就 可读,可写,可执行,,6说明当前用户可读可写不可执行第三位:group组用户,6的意 ...

  10. python_序列

    1. python存在6中内建序列:列表.元组.字符串.Unicode字符串.buffer对象.xrange对象 列表可以修改,元组和字符串不可以修改. 2. 序列支持的操作: 索引 序列中所有的元素 ...