在加载页面后使用AngularJs属性编译元素

时间:2018-02-03 06:45:54

标签: angularjs

我需要根据某些<a ng-click="controller.doThat(X)">调用的响应创建多个$http元素。并将它们添加到由其id属性指定的元素中。

我正在使用AngularJs v1.6.4。请帮忙。

1 个答案:

答案 0 :(得分:0)

var newElem = $compile(t)($scope);
document.getElementById("designatedDiv").appendChild(newElem[0]);

does the job as long as t is a string of a single element:

var t = "try and press <a ng-click=\"controller.doThat(X)\">here</a> and see..."

will result in:

Error: [jqLite:nosel] http://errors.angularjs.org/1.6.4/jqLite/nosel
    at angular.min.js:6

BUT if you redo the last example as following:

var t = "<span>try and press </span><a ng-click=\"controller.doThat(X)\">here</a><span> and see...</span>"
var newElem = $compile(t)($scope);
for (i=0;i<newElem.length;i++)
    document.getElementById("designatedDiv").appendChild(newElem[i]);

then it works as expected.