&是什么QUOT;要求"指令定义对象的需要? [英] What does "require" of directive definition object take?

查看:102
本文介绍了&是什么QUOT;要求"指令定义对象的需要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要 - 需要另一个控制器传递到当前指令
  联的功能。该要求取指令控制器的名字
  中通过。如果能找到这样的控制将引发一个错误。该名称可以与pfixed $ P $:

require - Require another controller be passed into current directive linking function. The require takes a name of the directive controller to pass in. If no such controller can be found an error is raised. The name can be prefixed with:


      
  • ? - 不产生错误。这使得需要依赖可选的。

  •   
  • ^ - 寻找在父元素控制器以及

  •   

以上是从官方文档的定义。这里的歧义是究竟什么是指令控制器。

Above is the definition from the official docs. The ambiguity here is what exactly is a "directive controller".

从angularjs的UI引导项目就拿标签指令作为一个例子

Take the tabs directive from the angularjs-ui bootstrap project, as an example.

angular.module('ui.bootstrap.tabs', [])
.controller('TabsController', ['$scope', '$element', function($scope, $element) {
  ... // omitted for simplicity
}])
.directive('tabs', function() {
  return {
    restrict: 'EA',
    transclude: true,
    scope: {},
    controller: 'TabsController',
    templateUrl: 'template/tabs/tabs.html',
    replace: true
  };
})
.directive('pane', ['$parse', function($parse) {
  return {
    require: '^tabs',
    restrict: 'EA',
    transclude: true,
    scope:{
      heading:'@'
    },
    link: function(scope, element, attrs, tabsCtrl) {
      ... // omitted for simplicity
    },
    templateUrl: 'template/tabs/pane.html',
    replace: true
  };
}]);

面板指令有要求:^标签,其中标签是它的父元素的指令的名称,而连接到该指令控制器的名称是 TabsController 。从上面的定义我自己的跨pretation,它应该是要求:^ TabsController不是要求:^标签而这显然是错误的。请告诉我什么我在我的COM prehension失踪。

The pane directive has require: '^tabs', in which tabs is the name of a directive on its parent element, while the name of the controller attached to that directive is TabsController. From my own interpretation of the above definition, it should have been require: '^TabsController' not require: '^tabs' and that's obviously wrong. Please tell me what am I missing in my comprehension.

推荐答案

文档的这一特定的主题确实是混乱的,但奇怪,因为它似乎是这一切才有意义。

This particular topic of the documentation is indeed confusing, however as strange as it seems to be it all makes sense.

要理解这个定义背后的逻辑,关键是要明白指令控制是指指令的控制器实例并没有一个控制器工厂。

The key to understand the logic behind this definition is to understand that "directive controller" refers to a directive's controller instance and not a controller factory.

继标签例如,当标签元素创建的 TabsController ,还会创建一个新的实例和连接到该特定元素的数据,是这样的:

Following the tabs example, when a tabs element is created, a new instance of the TabsController is also created and attached to that specific element data, something like:

tabElement.data('$tabsController', tabsControllerInstance)

要求:^标签面板元素基本上是针对特定的控制器实例的请求( tabsControllerInstance )被父用标签元素。

The require: '^tabs' on the pane element is basically a request for that specific controller instance (tabsControllerInstance) being used on the parent tabs element.

这篇关于&是什么QUOT;要求"指令定义对象的需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆