我需要根据日期来确定模板。我看到了一个很好的例子。 但在那个例子中,模板非常简单,因此可以使用字符串。在我的情况下,我想使用php来生成模板,所以我这样使用它:
eng.directive('vis', function ($compile) {
var getTemplate = function(ir) {
var k = (ir.visits.last && parseInt(ir.visits.last.done))?'V':'E';
var s = (ir.data.kind == 0)?'H':'V';
return s+k+'T';
}
var linker = function(scope, element, attrs) {
scope.$watch('ir',function(){
if (!scope.ir) return;
element.html(jQuery('#'+getTemplate(scope.ir)).html()).show();
$compile(element.contents())(scope);
})
}
return {
restrict: "E",
rep1ace: true,
link: linker
};});
模板如下:
<div id=HVT style="display:none">
<p>horizontal view template</p>
</div>
<div id=HET style="display:none">
<p>horizontal {{1+5}} Edit template</p>
</div>
<div id=VVT style="display:none">
<p>vertical view template</p>
</div>
<div id=VET style="display:none">
<p>vertical Edit template</p>
</div>
我确定有更聪明的方法。是否使用templateUrl更好?有人能告诉我如何在我的情况下使用它吗?
编辑:出了个问题。模板看不到scope。
function(el,attrs){ return '/tmpls/' + attrs.template; }
。 - m.e.conroy