AngularJS的directive(指令)配置选项说明
var appModule = angular.module("appModule", []);
appModule.controller("Ctrl", ["$scope", "$timeout", function($scope, $timeout) {
$scope.naomi = { name: "Naomi", address: "1600 Amphitheatre" };
$scope.igor = { name: "Igor", address: "123 Somewhere" };
$scope.vojta = { name: "Vojta", address: "3456 Somewhere Else" };
$scope.format = "M/d/yy h:mm:ss a";
$scope.name = "Tobias";
$scope.hideDialog = function () {
$scope.dialogIsHidden = true;
$timeout(function () {
$scope.dialogIsHidden = false;
}, 2000);
};
}]);
appModule.directive('myTabs', function() {
return {
restrict: 'E',
transclude: true,
scope: {},
controller: function($scope) {
var panes = $scope.panes = [];
$scope.select = function(pane) {
angular.forEach(panes, function(pane) {
pane.selected = false;
});
pane.selected = true;
};
this.addPane = function(pane) {
if (panes.length == 0) {
$scope.select(pane);
}
panes.push(pane);
};
},
templateUrl: 'my-tabs.html'
};
});
appModule.directive('myPane', function() {
return {
require: '^myTabs',
restrict: 'E',
transclude: true,
scope: {
title: '@'
},
link: function(scope, element, attrs, tabsCtrl) {
tabsCtrl.addPane(scope);
},
templateUrl: 'my-pane.html'
};
});
appModule.directive("myDraggable", ["$document", function($document) {
return function(scope, elem, attrs) {
var startX = 0, startY = 0, x = 0, y = 0;
elem.css({
position: "relative",
border: "1px solid red",
backgroundColor: "lightgrey",
cursor: "pointer"
});
elem.on("mousedown", function(event) {
// 组织所选对象的默认拖曳操作
event.preventDefault();
startX = event.pageX - x;
startY = event.pageY - y;
$document.on("mousemove", mousemove);
$document.on("mouseup", mouseup);
});
function mousemove(event) {
y = event.pageY - startY;
x = event.pageX - startX;
elem.css({
top: y + "px",
left: x + "px"
});
}
function mouseup() {
$document.off("mousemove", mousemove);
$document.off("mouseup", mouseup);
}
}
}]);
appModule.directive("myDialog", function() {
return {
restrict: "E",
transclude: true,
scope: {
"close": "&onClose"
},
templateUrl: "my-dialog.html",
link: function (scope, ele, attrs) {
scope.name = "Jeff";
}
};
});
appModule.directive("myCustomer", [function() {
return {
restrict: "E",
scope: {
customerInfo: "=info"
},
transclude: true,
template: "",
templateUrl: "tpls.html",
link: function(scope, element, attrs) {
}
};
}]);
appModule.directive("myCurrentTime", ["$timeout", "dateFilter", function($timeout, dateFilter) {
return {
link: function (scope, element, attrs) {
var format, timeoutId;
function updateTime() {
//使用内置dateFilter服务
element.text(dateFilter(new Date(), format));
//使用内置$filter服务
//element.text($filter("date")(new Date(), format));
}
scope.$watch(attrs.myCurrentTime, function(value) {
format = value;
updateTime();
});
function scheduleUpdate() {
timeoutId = $timeout(function() {
updateTime();
scheduleUpdate();
}, 1000);
}
element.on("$destroy", function() {
$timeout.cancel(timeoutId);
});
scheduleUpdate();
}
}
}]);
创建指令
$compile服务会查找一个名叫myTabs的控制器,如果没有找到,就会抛出一个错误。该选项的值可以分别用前缀?、^和?^进行修饰,也可以不修饰。使用^前缀意味着指令将会在它的父元素上面搜索控制器,使用?前缀就意味着如果在当前指令没有找到控制器,就将null作为link的第四个参数,如果将?和^结合起来,我们就可以既指定上游指令,又可以在找不到时,不抛出严重的错误。没有前缀修饰,指令默认只在所属元素上搜索指定的控制器。restrict
该选项指定创建指令的方式,创建方式有E(元素),A(属性),C(类名),M(注释),因此可以取值"E","EA","EAC","EACM"等等。最佳实践:最好通过标签名和属性来使用指令而不要通过注释和类名。这样做可以更容易地看出一个元素是跟哪个指令匹配的。通常注释式命名式指令使用在如下情景:某些指令需要跨越多个元素,但是受DOM API的限制,无法跨越多个元素(比如<table>元素)。 AngularJS 1.2 引入了ng-repeat-start和ng-repeat-end指令,作为更好的解决方案。 建议开发者使用这种方式,而不要用“自定义注释”形式的指令。什么情况下该用元素名,什么情况下该用属性名? 当创建一个含有自己模板的组件的时候,建议使用元素名,常见情况是,当你想为你的模板创建一个DSL(特定领域语言)的时候。如果仅仅想为已有的元素添加功能,建议使用属性名。
<div bind-to-this="thing">,你就要使用'=bindToThis'的绑定。scope的绑定策略:=代表与父作用域中的属性双向绑定,@代表把当前属性作为字符串传递,也可绑定父作用域的值,&代表传递一个来自父作用域的函数,稍后调用。element.on('$destroy', ...) 或scope.$on('$destroy', ...)来执行一个清理的工作。AngularJS的directive(指令)配置选项说明的更多相关文章
- angularjs中directive指令与component组件有什么区别?
壹 ❀ 引 我在前面花了两篇博客分别系统化介绍了angularjs中的directive指令与component组件,当然directive也能实现组件这点毋庸置疑.在了解完两者后,即便我们知道co ...
- AngularJS中Directive指令系列 - scope属性的使用
文章是转的,我做下补充.原文地址:https://segmentfault.com/a/1190000002773689 每当一个指令被创建的时候,都会有这样一个选择,是继承自己的父作用域(一般是外部 ...
- AngularJS中Directive指令系列 - 基本用法
参考: https://docs.angularjs.org/api/ng/service/$compile http://www.zouyesheng.com/angular.html Direct ...
- 关于Angularjs写directive指令传递参数
包子又来啦.... 在Angularjs当中,我们可能会经常要写directive指令.但是指令如果要共用的话,肯定是有细微的差别的,所以这些差别可能需要一个参数来决定 所以如何在指令中传递参数呢.. ...
- Angularjs之directive指令学习笔记(二)
1.Directive的五个实例知道driective作用.其中字段restrict.template. replace.transclude.link用法 参考文章链接地址:http://damoq ...
- 【angularJS】Directive指令
AngularJS 通过被称为 指令 的新属性来扩展 HTML.指令是扩展的 HTML 属性,带有前缀 ng-. 内置指令 1.ng-app 指令初始化一个 AngularJS 应用程序. 定义了 A ...
- angularjs学习之六(angularjs中directive指令的一般编程事件绑定 模板使用等)
angular js 中模板的使用.事件绑定以及指令与指令之间的交互 相应教学视频地址(需FQ):v=aG8VD0KvUw4">angularjs教学视频 <!doctype h ...
- AngularJS clone directive 指令复制
需求背景: directive模块化某表单信息,但表单信息可加入多条.此时就涉及到clone directive. 解决方式: 能够通过使用angularjs中$com ...
- AngularJS中Directive指令系列 - bindToController属性的使用
默认false.这个属性用来绑定scope的属性直接赋给controller.可以为true或者和scope相同格式的对象. 此外使用此属性,要设置controller的别名,通常通过"co ...
- AngularJS中Directive指令系列
近段时间在研究Angular中的directive用法,打算写个系列.以官方文档为主.并参考诸多教程.加上自己的思考. 基本概念及用法 scope属性的使用. &, <, =, @ 符 ...
随机推荐
- JAVA中生成Excel方法
java 操作 Excel 最常用的就是JXL(java excel api)和POI,今先看下JXL吧.首先可以到 http://www.andykhan.com/jexcelapi/downloa ...
- codevs1297 硬币
1297 硬币 题目描述 Description 我们知道即使是同一种面值的硬币,它们的重量也有可能不一样,因为它受到许多因素的影响,包括制造工艺和流程上的.但是任何一种面值的硬币的重量总是处于某 ...
- memcached完全剖析系列——一、memcached基础
转自:http://blog.charlee.li/memcached-001/
- ContentProvider官方教程(4)ContentResolver权限
Content Provider Permissions A provider's application can specify permissions that other application ...
- vs配置opencv
配置OpenCv: 一.新建工程 无论是mfc还是控制台程序都可以. 二.下载opencb,安装在非空格路径下面 解压出来有两个文件夹:build和source,主要用到build,build-x86 ...
- 如何快速清除.svn文件
Windows Registry Editor Version 5.00[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\清除SVN信息] @=&qu ...
- SqlSever基础 select cast 将一个int类型数据转换为char
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- 【leetcode❤python】Sum Of Two Number
#-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...
- (Theano 1)Theano自述文件
Theano在GitHub上的自述文件 https://github.com/Theano/Theano 也不知道这个Theano好不好,但是从Theano到Lasagne:基于Python的深度学习 ...
- servlet&jsp高级:第二部分
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...