Fre*_*ind 10 angularjs angularjs-directive
在本文档中有关指令:http://docs.angularjs.org/guide/directive
指令定义对象
指令定义对象向编译器提供指令.属性是:
name - 当前范围的名称.可选,默认为注册时的名称.
我不明白为什么名称是当前范围的名称?注册时的名字是什么?如果我指定名称,如何使用它?
app.directive('aaa', function() {
return {
name: 'bbb',
link: function() {
// how and where to use the new name `bbb`
}
}
}
Run Code Online (Sandbox Code Playgroud)
Liv*_* T. 18
在对源代码进行一些挖掘之后,这就是我发现的:这是一种声明一个单独的属性来动态地为控制器指定控制器的方法.见plunker.
我们的想法是将控制器引用放在与指令名称不同的属性中.如果未指定name属性,则将使用指令名称作为属性.
var app = angular.module('angularjs-starter', []);
app.directive('myDirective', [ function() {
return {
name : 'myController',
controller : '@',
restrict : 'A',
link : function(scope, elm, attr) {
console.log('myDirective.link');
}
};
} ]);
app.directive('myDirective2', [ function() {
return {
controller : '@',
restrict : 'A',
link : function(scope, elm, attr) {
console.log('myDirective2.link');
}
};
} ]);
app.controller('MyDirectiveController', [ '$scope', function($scope) {
console.log('MyDirectiveController.init');
} ]);
app.controller('MyDirectiveController2', [ '$scope', function($scope) {
console.log('MyDirectiveController2.init');
} ]);
app.controller('MyDirective2Controller', [ '$scope', function($scope) {
console.log('MyDirective2Controller.init');
} ]);
Run Code Online (Sandbox Code Playgroud)
模板:
<h1 my-directive my-controller="MyDirectiveController">My Directive Controller</h1>
<h1 my-directive my-controller="MyDirectiveController2">My Directive Controller 2</h1>
<h1 my-directive2="MyDirective2Controller">My Directive 2 Controller</h1>
Run Code Online (Sandbox Code Playgroud)
输出:
MyDirectiveController.init
myDirective.link
MyDirectiveController2.init
myDirective.link
MyDirective2Controller.init
myDirective2.link
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4789 次 |
| 最近记录: |