2016-02-02 116 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