2014-02-06 58 views

回答

0

,可以輸出相同的標記作爲第一個例子:

demo.directive("customTable",function(){ 
    return ({ 
      restrict : "A",    
      template: '<span ng-repeat="element in header" ng-style="{width: element.width}" class="box">{{element.column}}</span>' 
    }); 
    function link($scope, element, attributes){  
     console.log($scope.header); 
     console.log("now what"); 
    } 
}); 

這一個使用父範圍,所以你不需要標題屬性:

<h1>Display option 2: Use Directive </h1> 
    <div custom-table> 
    </div> 

這裏是一個updated plunker

您也可以使用隔離範圍和雙向綁定標題。這允許父範圍和指示範圍之間要綁定的頭對象:

demo.directive("customTable",function(){ 
    return ({ 
      restrict : "A", 
      scope: { 
       header = '=' 
      },   
      template: '<span ng-repeat="element in header" ng-style="{width: element.width}" class="box">{{element.column}}</span>' 
    }); 
    function link($scope, element, attributes){  
     console.log($scope.header); 
     console.log("now what"); 
    } 
}); 

然後你可以聲明header爲你做了最初:

<h1>Display option 2: Use Directive </h1> 
    <div custom-table header="header"> 
    </div> 
+0

達文..這工作正常較小的方案的東西..但我的最終目標是能夠對齊每列下面的值。請參閱此處的主題:http://stackoverflow.com/questions/21615477/aligning-elements-using-angularjs-directives – runtimeZero