角指令中的“角”定义?

前端之家收集整理的这篇文章主要介绍了角指令中的“角”定义?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在本文中关于指令: http://docs.angularjs.org/guide/directive

Directive Definition Object

The directive definition object provides instructions to the compiler. The attributes are:

nameName of the current scope. Optional and defaults to the name at registration.

我不明白为什么名字是当前范围的名称注册名称是什么?如果我指定一个名字,怎么用呢?

  1. app.directive('aaa',function() {
  2. return {
  3. name: 'bbb',link: function() {
  4. // how and where to use the new name `bbb`
  5. }
  6. }
  7. }
在一些挖掘源代码之后,这是我发现的:
这是一种声明一个单独的属性以动态地将控制器分配给该指令的方式.见 plunker.

这个想法是将控制器引用放在与指令名称不同的属性中.如果未指定name属性,则将使用该指令名作为属性.

  1. var app = angular.module('angularjs-starter',[]);
  2.  
  3. app.directive('myDirective',[ function() {
  4. return {
  5. name : 'myController',controller : '@',restrict : 'A',link : function(scope,elm,attr) {
  6. console.log('myDirective.link');
  7. }
  8. };
  9. } ]);
  10.  
  11. app.directive('myDirective2',[ function() {
  12. return {
  13. controller : '@',attr) {
  14. console.log('myDirective2.link');
  15. }
  16. };
  17. } ]);
  18.  
  19. app.controller('MyDirectiveController',[ '$scope',function($scope) {
  20. console.log('MyDirectiveController.init');
  21. } ]);
  22.  
  23. app.controller('MyDirectiveController2',function($scope) {
  24. console.log('MyDirectiveController2.init');
  25. } ]);
  26.  
  27. app.controller('MyDirective2Controller',function($scope) {
  28. console.log('MyDirective2Controller.init');
  29. } ]);

模板:

  1. <h1 my-directive my-controller="MyDirectiveController">My Directive Controller</h1>
  2. <h1 my-directive my-controller="MyDirectiveController2">My Directive Controller 2</h1>
  3. <h1 my-directive2="MyDirective2Controller">My Directive 2 Controller</h1>

输出

  1. MyDirectiveController.init
  2. myDirective.link
  3. MyDirectiveController2.init
  4. myDirective.link
  5. MyDirective2Controller.init
  6. myDirective2.link

猜你在找的Angularjs相关文章