jen*_*gar 6 javascript angularjs
我想创建一个指令来组织按日期分组的显示数据.我还希望能够指定一个显示各行的指令.在一个完美的世界里,它会看起来像这样(但很漂亮)
Friday, Oct 28
[some directive html]
[some directive html]
[some directive html]
Saturday, Oct 29
[some directive html]
Sunday, Oct 30
[some directive html]
[some directive html]
...
Run Code Online (Sandbox Code Playgroud)
这显然不起作用,所以如果你有更好的方法,请告诉我,但我希望能够按照这些方针做一些事情:
app.directive('dateOrganized', [function(){
return {
template:
'<div>' +
'<div ng-repeat="organizedDate in organizedDate">' +
'<div>{{organizedDate.date | date}}</div>' +
'<div ng-repeat="item in organizedDate.items">' +
'{{rowDirectiveHtml}}' +
'</div>' +
'</div>' +
'</div>',
scope: {
organizedDates: '=',
rowDirectiveHtml: '='
}
...
};
}])
app.directive('itemRow', [function(){
return {
template: '<div>{{item.data}}</div>',
scope: {
item: '='
}
};
}]);
Run Code Online (Sandbox Code Playgroud)
然后像这样使用它:
<div data-organized organized-dates="stuff" row-directive-html="<div item-row item=\"item\" />" />
Run Code Online (Sandbox Code Playgroud)
我知道这是非常丑陋的(并且不起作用,但我确信我可以通过一些调整来实现它)所以我真正要问的是,有更好的方法吗?
New*_*Dev 12
这个问题比看起来更复杂,所以让我们把它分解.
您正在构建的是一个接受部分模板的指令<div item-row item="item" />- 并且该模板使用(或与范围链接)内部变量 - item用户未在外部范围中定义; 它的含义由您的指令定义,您的用户通过阅读指令的文档来"发现"它.我通常用前缀命名这样的"魔术"变量$,例如$item.
步骤1
不是通过属性绑定将模板作为HTML-as-string传递,而是将其作为内容传递并转换该内容.Transcluding允许您将transcluded内容绑定到任意范围:
<foo>
<div>my item is: {{$item}}</div>
</foo>
Run Code Online (Sandbox Code Playgroud)
.directive("foo", function(){
return {
scope: {},
transclude: true,
template: "<h1>I am foo</h1><placeholder></placeholder>",
link: function(scope, element, attrs, ctrls, transclude){
scope.$item = "magic variable";
transclude(scope, function(clonedContent){
element.find("placeholder").replaceWith(clonedContent);
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
上面将放置<div>my item is: {{$item}}</div>指令所foo决定的模板(可以是您指定的任何模板),并将链接到已$item定义的范围.
第2步
但是你的指令增加的复杂性是它使用ng-repeat,它本身接受一个模板,你的指令接收的模板需要用作模板ng-repeat.
只有上面的方法,这是行不通的,因为到时候link运行,ng-repeat在你有机会申请你的内容之前,它已经已经转换了自己的内容.
解决这个问题的一种方法是手动$compile模板foo而不是使用template属性.在编译之前,我们将有机会在需要的地方放置预期的模板:
.directive("foo", function($compile){
return {
scope: {},
transclude: true,
link: function(scope, element, attrs, ctrls, transclude){
scope.items = [1, 2, 3, 4];
var template = '<h1>I am foo</h1>\
<div ng-repeat="$item in items">\
<placeholder></placeholder>\
</div>';
var templateEl = angular.element(template);
transclude(scope, function(clonedContent){
templateEl.find("placeholder").replaceWith(clonedContent);
$compile(templateEl)(scope, function(clonedTemplate){
element.append(clonedTemplate);
});
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2893 次 |
| 最近记录: |