Directive多是AngularJS中比较复杂的一个东西了。通常咱们将其理解成指令。AngularJS自带了很多预设的指令,好比ng-app,ng-controller这些。能够发现个特色,AngularJS自带的指令都是由ng-打头的。javascript
那么,Directive到底是个怎么样的一个东西呢?我我的的理解是这样的:将一段html、js封装在一块儿,造成一个可复用的独立个体,具体特定的功能。下面咱们来详细解读一下Directive的通常性用法。html
var myDirective = angular.module('directives', []);
myDirective.directive('directiveName', function($inject) {
return {
template: '<div></div>',
replace: false,
transclude: true,
restrict: 'E',
scope: {},
controller: function($scope, $element) {
},
complie: function(tElement, tAttrs, transclude) {
return {
pre: function preLink(scope, iElement, iAttrs, controller) {
},
post: function postLink(scope, iElement, iAttrs, controller) {
}
};
},
link: function(scope, iElement, iAttrs) {
}
};
});
复制代码
return {
name: '',
priority: 0,
terminal: true,
scope: {},
controller: fn,
require: fn,
restrict: '',
template: '',
templateUrl: '',
replace: '',
transclude: true,
compile: fn,
link: fn
}
复制代码
如上所示,return的对象中会有不少的属性,这行属性都是用来定义directive的。java
name
jquery
priority
angularjs
teminal
express
scope
segmentfault
true
{}
controller
bash
require
app
restrict
异步
template
templateUrl
replace
transclude
true/false
转换这个directive的内容。(这个感受上,是直接将内容编译后搬入指定地方)‘element’
转换整个元素,包括其余优先级较低的directive。(像将总体内容编译后,看成一个总体(外面再包裹p),插入到指定地方)compile
link
这里关于directive的scope为一个object时,有更多的内容很是有必要说明一下。看下面的代码:
scope: {
name: '=',
age: '=',
sex: '@',
say: '&'
}
复制代码
这个scope中关于各类属性的配置出现了一些奇怪的前缀符号,有=,@,&,那么这些符号具体的含义是什么呢?再看下面的代码:
<div my-directive name="myName" age="myAge" sex="male" say="say()"></div>
复制代码
function Controller($scope) {
$scope.name = 'Pajjket';
$scope.age = 99;
$scope.sex = '我是男的';
$scope.say = function() {
alert('Hello,我是弹出消息');
};
}
复制代码
=
: 指令中的属性取值为Controller中对应$scope上属性的取值@
: 指令中的取值为html中的字面量/直接量&
: 指令中的取值为Controller中对应$scope上的属性,可是这个属性必须为一个函数回调 下面是更加官方的解释:=
或者=expression/attr
例如: 中,widget定义的scope为:{localModel: '=myAttr'},那么widget scope property中的localName将会映射父scope的parentModel。若是parentModel发生任何改变,localModel也会发生改变,反之亦然。即双向绑定。
@或者@attr 创建一个local scope property到DOM属性的绑定。由于属性值老是String类型,因此这个值总返回一个字符串。若是没有经过@attr指定属性名称,那么本地名称将与DOM属性的名称一致。 例如: ,widget的scope定义为:{localName: '@myAttr'}。那么,widget scope property的localName会映射出"hello "转换后的真实值。当name属性值发生改变后,widget scope的localName属性也会相应的改变(仅仅是单向,与上面的=不一样)。那么属性是在父scope读取的(不是从组件的scope读取的)
&或者&attr 提供一个在父scope上下文中执行一个表达式的途径。若是没有指定attr的名称,那么local name将与属性名一致。
<widget my-attr="count = count + value">
,widget的scope定义为:{localFn:’increment()’},那么isolate scope property localFn会指向一个包裹着increment()表达式的function。 通常来讲,咱们但愿经过一个表达式,将数据从isolate scope传到parent scope中。这能够经过传送一个本地变量键值的映射到表达式的wrapper函数中来完成。例如,若是表达式是increment(amount),那么咱们能够经过localFn({amount:22})的方式调用localFn以指定amount的值。
下面的示例都围绕着上面所做的参数说明而展开的。
// 自定义directive
var myDirective = angular.modeule('directives', []);
myDirective.directive('myTest', function() {
return {
restrict: 'EMAC',
require: '^ngModel',
scope: {
ngModel: '='
},
template: '<div><h4>Weather for {{ngModel}}</h4</div>'
};
});
// 定义controller
var myControllers = angular.module('controllers', []);
myControllers.controller('testController', [
'$scope',
function($scope) {
$scope.name = 'this is directive1';
}
]);
var app = angular.module('testApp', [
'directives',
'controllers'
]);
<body ng-app="testApp">
<div ng-controller="testController">
<input type="text" ng-model="city" placeholder="Enter a city" />
<my-test ng-model="city" ></my-test>
<span my-test="exp" ng-model="city"></span>
<span ng-model="city"></span>
</div>
</body>
复制代码
templateUrl其实根template功能是同样的,只不过templateUrl加载一个html文件,上例中,咱们也能发现问题,template后面根的是html的标签,若是标签不少呢,那就比较不爽了。能够将上例中的,template改一下。
myDirective.directive('myTest', function() {
return {
restrict: 'EMAC',
require: '^ngModel',
scope: {
ngModel: '='
},
templateUrl:'../partials/tem1.html' //tem1.html中的内容就是上例中template的内容。
}
});
复制代码
//directives.js中定义myAttr
myDirectives.directive('myAttr', function() {
return {
restrict: 'E',
scope: {
customerInfo: '=info'
},
template: 'Name: {{customerInfo.name}} Address: {{customerInfo.address}}<br>' +
'Name: {{vojta.name}} Address: {{vojta.address}}'
};
});
//controller.js中定义attrtest
myControllers.controller('attrtest',['$scope',
function($scope) {
$scope.naomi = {
name: 'Naomi',
address: '1600 Amphitheatre'
};
$scope.vojta = {
name: 'Vojta',
address: '3456 Somewhere Else'
};
}
]);
// html中
<body ng-app="testApp">
<div ng-controller="attrtest"> <my-attr info="naomi"></my-attr> </div> </body>
复制代码
Name: Naomi Address: 1600 Amphitheatre //有值,由于customerInfo定义过的
Name: Address: //没值 ,由于scope重定义后,vojta是没有定义的
复制代码
myDirectives.directive('myAttr', function() {
return {
restrict: 'E',
template: 'Name: {{customerInfo.name}} Address: {{customerInfo.address}}<br>' +
'Name: {{vojta.name}} Address: {{vojta.address}}'
};
});
复制代码
Name: Address:
Name: Vojta Address: 3456 Somewhere Else
复制代码
由于此时的directive没有定义独立的scope,customerInfo是undefined,因此结果正好与上面相反。
myDirective.directive('myEvent', function() {
return {
restrict: 'E',
transclude: true,
scope: {
'close': '$onClick' //根html中的on-click="hideDialog()"有关联关系
},
templateUrl: '../partials/event_part.html'
};
});
myController.controller('eventTest', [
'$scope',
'$timeout',
function($scope, $timeout) {
$scope.name = 'Tobias';
$scope.hideDialog = function() {
$scope.dialogIsHidden = true;
$timeout(function() {
$scope.dialogIsHidden = false;
}, 2000);
};
}
]);
复制代码
<body ng-app="phonecatApp">
<div ng-controller="eventtest">
<my-event ng-hide="dialogIsHidden" on-click="hideDialog()">
Check out the contents, {{name}}!
</my-event>
</div>
</body>
<!--event_part.html -->
<div>
<a href ng-click="close()">×</a>
<div ng-transclude></div>
</div>
复制代码
<body ng-app="phonecatApp">
<div ng-controller="eventtest">
<div ng-hide="dialogIsHidden" on-click="hideDialog()">
<span>Check out the contents, {{name}}!</span>
</div>
</div>
</body>
复制代码
controller
,link
,compile
之间的关系myDirective.directive('exampleDirective', function() {
return {
restrict: 'E',
template: '<p>Hello {{number}}!</p>',
controller: function($scope, $element){
$scope.number = $scope.number + "22222 ";
},
link: function(scope, el, attr) {
scope.number = scope.number + "33333 ";
},
compile: function(element, attributes) {
return {
pre: function preLink(scope, element, attributes) {
scope.number = scope.number + "44444 ";
},
post: function postLink(scope, element, attributes) {
scope.number = scope.number + "55555 ";
}
};
}
}
});
//controller.js添加
myController.controller('directive2',[
'$scope',
function($scope) {
$scope.number = '1111 ';
}
]);
//html
<body ng-app="testApp">
<div ng-controller="directive2"> <example-directive></example-directive> </div> </body>
复制代码
Hello 1111 22222 44444 5555 !
复制代码
由结果能够看出来,controller先运行,compile后运行,link不运行。 咱们如今将compile属性注释掉后,获得的运行结果以下:
Hello 1111 22222 33333
!