【发布时间】:2016-12-09 20:52:44
【问题描述】:
我对 AngularJS 还很陌生,遇到了当前的问题。
我的控制器中有以下内容
//Define the programs list table columns
$scope.programListColumns = "[{ Name: 'Name'},{ Status: 'Status'},{ CreatedByUserName: 'Created By'},{ ModifiedDate: 'Modified'},{ ModifiedByUserName: 'Modified By'}]";
我想在 html 中将其用作我的元素的列图,就像这样...
<table-helper datasource="programsList" columnmap="{{programListColumns}}"></table-helper>
我的指令非常广泛,但我基本上是从我的数据源构建一个表,并使用我的列映射从中映射我想要的数据,如果有意义的话,为每个项目创建标题和行。
这是我的指令缩写...
(function(){
var app = angular.module("MdfAngularApp");
var tableHelper = function() {
//Initiate variables for directive
//var template = '<div class="tableHelper"></div>',
var link = function(scope, element, attrs) {
var headerCols = [],
tableStart = '<table>',
tableEnd = '</table>',
table = '',
visibleProps = [],
sortCol = null,
sortDir = 1;
//Watch the datasource for changes.
scope.$watchCollection('datasource', render);
... Functions go here ...
return {
restrict: 'E',
replace: true,
scope: {
columnmap: '@',
datasource: '='
},
link: link,
}
};
app.directive('tableHelper', tableHelper);
}());
执行上述操作后,我得到一个空字符串作为列图。
现在,如果我把我的 html 写成这样
<table-helper datasource="programsList" columnmap="[{ Name: 'Name'},{ Status: 'Status'},{ CreatedByUserName: 'Created By'},{ ModifiedDate: 'Modified'},{ ModifiedByUserName: 'Modified By'}]"></table-helper>
并将我的隔离范围列映射属性更改为“=”,一切都很好。我只是想比这更封装一点。
任何帮助将不胜感激。
【问题讨论】:
标签: angularjs using-directives isolate-scope