一般来讲 directive名字遵循一下规则:

1.忽略以x-和data-为元素/属性的前缀

2.转化“:”,“-”,“_”命名为驼峰命名

如下所示

<div ng-controller="Controller">
Hello <input ng-model='name'> <hr/>
<span ng-bind="name"></span> <br/>
<span ng:bind="name"></span> <br/>
<span ng_bind="name"></span> <br/>
<span data-ng-bind="name"></span> <br/>
<span x-ng-bind="name"></span> <br/>
</div>
angular.module('docsBindExample', [])
.controller('Controller', ['$scope', function($scope) {
$scope.name = 'Max Karl Ernst Ludwig Planck (April 23, 1858 – October 4, 1947)';
}]);

transclude makes the contents of a directive with this option have access to the scope outside of the directive rather than inside.

(ng-transclude="true" 是当前directive模板内容访问的是外部scope而不是内部scope)

示例:

script.js

angular.module('docsTransclusionExample', [])
.controller('Controller', ['$scope', function($scope) {
$scope.name = 'Tobias';
}])
.directive('myDialog', function() {
return {
restrict: 'E',
transclude: true,
scope: {},
templateUrl: 'my-dialog.html',
link: function (scope) {
scope.name = 'Jeff';
}
};
});
index.html

<div ng-controller="Controller">
<my-dialog>Check out the contents, {{name}}!</my-dialog>
</div>
my-dialog.html

<div class="alert" ng-transclude></div>

运行结果:Check out the contents, Tobias!

The transclude option changes the way scopes are nested. It makes it so that the contents of a transcluded directive have whatever scope is outside the directive, rather than whatever scope is on the inside. In doing so, it gives the contents access to the outside scope.

如果想以上代码运行结果为:Check out the contents, Jeff!

则directive不能建立自己独立scope:

script.js

angular.module('docsTransclusionExample', [])
.controller('Controller', ['$scope', function($scope) {
$scope.name = 'Tobias';
}])
.directive('myDialog', function() {
return {
restrict: 'E',
transclude: true,
//scope: {},
templateUrl: 'my-dialog.html',
link: function (scope) {
scope.name = 'Jeff';//scope继承父scope
}
};
});

directive中require选项,比如require:"^^myTabs",“^^”前缀意味着myTabs会在父元素中的controller查找,单个“^”意味着在自身或者父元素中查找,没有前缀则只在自身中查找,以上如果没有查找到就会抛错。

如果directive中需要包含多个controller,则link function第四个参数为一个数组类型

angular.module('docsTabsExample', [])
.directive('myPane', function() {
return {
require: ['^^myTabs', 'ngModel'],
restrict: 'E',
transclude: true,
scope: {
title: '@'
},
link: function(scope, element, attrs, controllers) {
var tabsCtrl = controllers[0],
modelCtrl = controllers[1]; tabsCtrl.addPane(scope);
},
templateUrl: 'my-pane.html'
};
});

controller和link的不同之处在于:controller向外暴露API,link函数通过require选项与controller交互

angular directive知识的更多相关文章

  1. Vue.js中Directive知识

    近期所学的Vue.js这个MVVM前端技术缓解了我一直愁于前后端开发杂糅所带来的痛苦.今天就来说说关于Vue.js里面的Directive知识. Directive Directive看上去虽然和An ...

  2. angular directive scope

    angular directive scope 1.当directive 中不指定scope属性,则该directive 直接使用 app 的scope: 2.当directive 中指定scope属 ...

  3. angular directive指令内的参数

    angular.module('myApp', []) .directive('myDirective', function() { return { restrict: String, priori ...

  4. angular directive指令的复用

    “指令之之所以要定义成指令就是为了复用!” 指令一定是可以用在不同的controller里面的,为了在不同的controller去使用它,我们一定要给指定的配置项一个指令.这样才能跟外面的控制器进行交 ...

  5. 使用 angular directive 和 json 数据 D3 随着标签 donut chart演示样本

    使用angular resource载入中priorityData.json中间json数据,结合D3绘制甜甜圈图.执行index.html其结果见于图.: priorityData.json中jso ...

  6. [Angular Directive] Assign a Structual Directive a Dynamic Context in Angular 2

     Just like passing in an array to *ngFor, you can pass in any value into your structural directive s ...

  7. [Angular Directive] Implement Structural Directive Data Binding with Context in Angular

    Just like in *ngFor, you're able to pass in data into your own structural directives. This is done b ...

  8. angular directive

    1.restrict (字符串)可选参数,指明指令在DOM里面以什么形式被声明: 取值有:E(元素),A(属性),C(类),M(注释),其中默认值为A: E(元素):<directiveName ...

  9. angular directive自定义指令

    先来看一下自定义指令的写法 app.directive('', ['', function(){ // Runs during compile return { // name: '', // pri ...

随机推荐

  1. Windows10-安装笔记

    Windows10-安装笔记... ------------------------------ ---------------------------------------- 加载镜像.加载完后. ...

  2. python 打印三级菜单

    要求: 1.用户选择城市菜单编号显示对应的下级菜单 2.可以返回上级菜单,只有在用户确定正常退出时才退出 #!/usr/bin/env python3 # -*- coding: utf-8 -*- ...

  3. 微信小程序结合后台数据管理实现商品数据的动态展示、维护

    微信小程序给我们提供了一个很好的开发平台,可以用于展现各种数据和实现丰富的功能,本篇随笔介绍微信小程序结合后台数据管理实现商品数据的动态展示.维护,介绍如何实现商品数据在后台管理系统中的维护管理,并通 ...

  4. 使用jmeter进行批量数据创建

    背景: 测试环境需要创建大量的测试数据,进行功能和性能的测试 测试数据创建接口是HTTP请求方式 测试数据要求不同类型的数据,要有字段进行关联,且单据的编号在DB中唯一不可重复,此外测试数据的时间类参 ...

  5. 两个input在同一行连着不留缝隙

    方法1:让两个input 连在一起写 不换行 <div class="inputDiv"> <input type="text" placeh ...

  6. 我的前端故事----关于redux的一些思考

    背景 我一个前端,今年第一份工作就是接手一个 APP 的开发...一个线下 BD 人员用的推广 APP,为了让我这个一天原生开发都没有学过的人能快速开发上线,于是乎就选择了 react-native ...

  7. JS判断当前使用设备是pc端还是web端(转MirageFireFox)

    js判断当前设备 最近用bootstrap做自适应,发现仍然很难很好的兼容web端和PC端的现实. 仔细观察百度,淘宝,京东等大型网站,发现这些网站都有对应不同客户端的子站. 站点 PC端url we ...

  8. Linux的netstat查看端口是否开放见解(0.0.0.0与127.0.0.1的区别)

    linux运维都需要对端口开放查看  netstat 就是对端口信息的查看 # netstat -nltp p 查看端口挂的程序 [root@iz2ze5is23zeo1ipvn65aiz ~]# n ...

  9. WebApi Ajax 跨域请求解决方法(CORS实现)(作者:jianxuanbing)

    概述 ASP.NET Web API 的好用使用过的都知道,没有复杂的配置文件,一个简单的ApiController加上需要的Action就能工作.但是在使用API的时候总会遇到跨域请求的问题,特别各 ...

  10. 第1阶段——uboot分析之通过nand命令读内核(8)

    本节主要学习: 详细分析UBOOT中"bootcmd=nand read.jffs2 0x30007FC0 kernel;bootm 0x30007FC0" 怎么实现nand命令读 ...