2016-02-02 32 views
4

我想创建一个新指令到ui.boostrap.accordion模块中以避免手风琴打开单击事件。angular-bootstrap添加新指令dosn't工作

我在另一个file.js下面的代码:

angular.module('ui.bootstrap.accordion') 
.directive('accordionGroupLazyOpen', function() { 
    return { 
    require: '^accordion',   
    restrict: 'EA', 
    transclude: true,    
    replace: true,     
    templateUrl: function(element, attrs) { 
     return attrs.templateUrl || 'template/accordion/accordion-group.html'; 
    }, 
    scope: { 
     heading: '@',    
     isOpen: '=?', 
     isDisabled: '=?' 
    }, 
    controller: function() { 
     this.setHeading = function(element) { 
     this.heading = element; 
     }; 
    }, 
    link: function(scope, element, attrs, accordionCtrl) { 
     accordionCtrl.addGroup(scope); 

     scope.openClass = attrs.openClass || 'panel-open'; 
     scope.panelClass = attrs.panelClass; 
     scope.$watch('isOpen', function(value) { 
     element.toggleClass(scope.openClass, value); 
     if (value) { 
      accordionCtrl.closeOthers(scope); 
     } 
     }); 

     scope.toggleOpen = function($event) { 
     }; 
    } 
    }; 
}) 

问题是,当我执行的应用程序,我得到以下错误:

Controller 'accordionGroup', required by directive 'accordionTransclude', can't be found!

错误link

任何想法?

+0

您的指令模板是否需要控制器'accordionGroup'?如果是这样,你是否在任何地方定义了一个控制器'accordionGroup'? –

+0

我已经复制了该指令,通过accordionGroupLazyOpen更改了名称accordionGroup并禁用了toggleOpen函数,因为它没有做任何事情。这是它做的唯一改变。更改名称并清空toggleOpen功能。 – Serginho

+0

显然还有一些其他指令需要AccordionGroup,因为你重命名了它,所以他们找不到它。 – koox00

回答

2

当我从source code看到(也许不是你的版本,但仍然是相同的):

// Use in the accordion-group template to indicate where you want the heading to be transcluded 
// You must provide the property on the accordion-group controller that will hold the transcluded element 
.directive('uibAccordionTransclude', function() { 
    return { 
    require: '^uibAccordionGroup', // <- look at this line in your version 
    link: function(scope, element, attrs, controller) { 
     scope.$watch(function() { return controller[attrs.uibAccordionTransclude]; }, function(heading) { 
     if (heading) { 
      element.find('span').html(''); 
      element.find('span').append(heading); 
     } 
     }); 
    } 
    }; 

所以我想它试图找到匹配accordionGroup视图的父指令,但由于添加了accordionGroupLazyOpen而不是accordionGroup它找不到它。如果您在accordion-group-template文件看,你会看到accordionTransclude指令被称为有

This error occurs when HTML compiler tries to process a directive that specifies the require option in a directive definition, but the required directive controller is not present on the current DOM element (or its ancestor element, if^was specified).

在错误页面,您所提供的状态。

+0

好点!我有更多的问题来适应手风琴。最后,一切运作良好。 – Serginho