[英]AngularJS Extending Default Options before Compiling
我正在建立一個簡單的表指令。 如果未在選項中傳遞,我需要使用默認寬度擴展列。
當前,我將選項復制並擴展到新的columns數組上,這樣可以避免設置一次列,然后再次重置它們,從而導致2個摘要。
有沒有一種理想的方法可以將這些擴展到現有的對象預編譯中,從而避免雙重摘要命中?
class DataTable { constructor($scope){ var ColumnDefaults = { width: 50 }; $scope.columns = $scope.options.columns.map((c) => { return angular.extend(angular.copy(ColumnDefaults), c); }); } } function Directive(){ return { restrict: 'E', controller: 'DataTable', scope: { options: '=', values: '=', onSelect: '&' }, controllerAs: 'dt', template: `<div class="dt material"> <dt-header columns="columns"></dt-header> <dt-body values="values" columns="columns"></dt-body> <dt-footer></dt-footer> </div>`, link: function($scope, $elm, $attrs){ //console.log($scope.options) } }; };
如果您掛接到指令編譯的pre函數,則可以在指令初始化之前獲得dom的權限。
compile: function(tElem, tAttrs){
return {
pre: function($scope, $elm, $attrs, ctrl){
... code here ...
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.