简体   繁体   English

AngularJS动态模板未评估

[英]AngularJS dynamic template not evaluated

I am dynamically generating a table template in a directive, in the compile attribute : 我在指令的compile属性中动态生成表模板:

compile : function (tElem, tAttrs, test) {
$http.get(tAttrs["source"]+"/0/1/true").success(
    function(result) {

        // adding angular expressions for adding data in table
        var tr = '<tr ng-repeat="item in data track by $index" >';
        for (var name in result[0]) {
            console.log(name);
            tr += '<td>{{ item.'+name+' }}</td>';
        }
        tElem.find("tbody").append(tr+'</tr>');

});
return function(){ ... }
}

But the expressions are not evaluated and I only get 但是表达式不求值,我只能得到

{{ item.id }}   {{ item.currMtmDate }}  {{ item.prevMtmDate }}  {{ item.user }} {{ item.description }}

displayed on my page. 显示在我的页面上。

If I do 如果我做

var v = 'currMtmDate';
var b = '<tr ng-repeat="item in data track by $index"><td>{{ item.'+v+' }}</td></tr>';
tElem.find("tbody").append(b);

then it works well. 那么效果很好。

I have no idea why it works in 2nd example and not in the 1st. 我不知道为什么它在第二个示例而不是第一个示例中起作用。

这样做的一种好方法是使用ng-include标记创建一个div并更新该标记: div ng-iclude="templateUrl"></div>然后在$ http末尾更新变量'templateUrl' 。得到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM