2015-11-08 119 views
0

我已經查看了questionsdocumentation以擴展第三方Angular指令。我真正需要做的是擴展第三方的Angular控制器。擴展第三方Angular控制器

我在我的應用程序中使用了Smart Table庫,並對其執行list row selection with checkboxes的建議。然而,我的應用程序需要兩種類型的選擇:

  1. 複選框選擇,其中複選框可用於選擇單個或多個行(已經覆蓋在上面的鏈接csSelectAll指令)
  2. 行突出顯示,其中只有一行可以突出顯示

基於select method,我很容易爲stTableController編寫高亮功能。但我希望能夠做到這一點,而不用自己動手智能表。

我試過extending controllers的解決方案,但它似乎假定父母和孩子控制器住在同一個模塊中。當我嘗試下面的代碼:

angular.module('myApp') 
    .controller('smarterTableController', function(<required deps>) { 
    $controller('smartTableController', {$scope: $scope})); 
    //initialization stuff left out... 
    this.highlight = function(row) { 
     var rows = copyRefs(displayGetter($scope)); 
     var index = rows.indexOf(row); 
     if (index !== -1) { 
     row.isHighlighted = row.isHighlighted !== true; 
     if (lastHighlighted) { 
      lastHighlighted.isHighlighted = false; 
     } 
     lastHighlighted = row.isHighlighted === true ? row : undefined; 
     } 
    }; 
    }); 
    //corresponding extension of the stTable directive to use the smarterTableController 

雖然此解決方案似乎不起作用。當我打電話使用$controller父控制器的功能,我得到的錯誤:

Argument 'smartTableController' is not a function, got undefined

這裏是我的兩個問題:

  1. 我應該能夠以這種方式擴展第三方控制器?
  2. 如果不是,分叉項目並添加這個額外的功能我唯一的其他選擇?
+0

關於第二個問題,這是一種做pull請求在你修改後的主分支上;-) –

+0

當然,我肯定會對原始項目開放一個問題,詢問他們是否對我的更改感興趣,如果他們想要他們,請貢獻一下。畢竟,我不想爲這件事維持我自己的分叉:-)。但我不知道常常需要選擇*和*突出顯示,所以我不想立即創建拉取請求來放肆。 – Jay

回答

0

我想你應該使用require參數創建一個自定義指令。

看一看的文檔在這裏:https://docs.angularjs.org/guide/directive#creating-directives-that-communicate

angular.module('myApp').directive("smarterTable", function(){ 
    return { 
     require:"smartTable", 
     link: function(arg1, arg2, arg3, smartTableController){ 
      // Put your logic here 
     } 
    } 


}); 

而在你看來,你將不得不補充:

<smart-table smarter-table=""></smart-table> 
+0

我會試試這個。現在我已經制作了自己的叉子,因爲它更快。你的解決方案看起來更好,但我還不知道它是如何工作的,可能直到我真正實現它纔可能理解。 – Jay

+0

當您創建指令時,參數'require'在同一元素上查找控制器。然後它將控制器對象(或範圍)傳遞給您的鏈接函數(即在控制器之後的指令生命週期執行器中)。 –