angular directive知识
一般来讲 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知识的更多相关文章
- Vue.js中Directive知识
近期所学的Vue.js这个MVVM前端技术缓解了我一直愁于前后端开发杂糅所带来的痛苦.今天就来说说关于Vue.js里面的Directive知识. Directive Directive看上去虽然和An ...
- angular directive scope
angular directive scope 1.当directive 中不指定scope属性,则该directive 直接使用 app 的scope: 2.当directive 中指定scope属 ...
- angular directive指令内的参数
angular.module('myApp', []) .directive('myDirective', function() { return { restrict: String, priori ...
- angular directive指令的复用
“指令之之所以要定义成指令就是为了复用!” 指令一定是可以用在不同的controller里面的,为了在不同的controller去使用它,我们一定要给指定的配置项一个指令.这样才能跟外面的控制器进行交 ...
- 使用 angular directive 和 json 数据 D3 随着标签 donut chart演示样本
使用angular resource载入中priorityData.json中间json数据,结合D3绘制甜甜圈图.执行index.html其结果见于图.: priorityData.json中jso ...
- [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 ...
- [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 ...
- angular directive
1.restrict (字符串)可选参数,指明指令在DOM里面以什么形式被声明: 取值有:E(元素),A(属性),C(类),M(注释),其中默认值为A: E(元素):<directiveName ...
- angular directive自定义指令
先来看一下自定义指令的写法 app.directive('', ['', function(){ // Runs during compile return { // name: '', // pri ...
随机推荐
- 获取windows所有用户名
#include <LM.h> #pragma comment(lib, "netapi32.lib") // See more: http://msdn.micros ...
- hdu 6171---Admiral(双向搜索)
题目链接 Problem Description Suppose that you are an admiral of a famous naval troop. Our naval forces h ...
- javascript常用函数实现的收集
收集了一些比较常用的javascript函数. 1.字符串长度截取 function cutstr(str, len) { var temp, icount = 0, patrn = /[^\x00- ...
- jboss初体验
本人电脑的java版本是java8,而jboss的版本最多支持到java7.x,导致启动jboss7,在浏览器无法访问localhost:8080. 于是我查找百度,发现jboss8其实就是wildf ...
- Tensorflow开发环境配置及其基本概念
Tensorflow开发环境配置及其基本概念 1.1. 安装Tensorflow开发环境 1.1.1. 安装pycharm 1.1.2. 安装pythe3.6 1.1.3. 安装Tensorflow ...
- DispatcherTimer和Timer(计时器)
System.Windows.Threading.DispatcherTimer dTime; System.Timers.Timer timer; public Main ...
- 表单校验demo
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- mysql中group by 的用法解析
1. group by的常规用法 group by的常规用法是配合聚合函数,利用分组信息进行统计,常见的是配合max等聚合函数筛选数据后分析,以及配合having进行筛选后过滤. 假设现有数据库表如下 ...
- jvm系列 (四) ---强、软、弱、虚引用
java引用 目录 jvm系列(一):jvm内存区域与溢出 jvm系列(二):垃圾收集器与内存分配策略 jvm系列(三):锁的优化 我的博客目录 为什么将引用分为不同的强度 因为我们需要实现这样一种情 ...
- ThreadLoacl的反思
在我的随笔 spring mvc:注解@ModelAttribue妙用 中使用ThreadLocal来简化spring mvc控制层controller中的ModelMap,Response.Jso ...