【发布时间】:2015-03-03 04:26:56
【问题描述】:
我无法让 $compile 手动使用 ng-repeat。这里有一个类似的问题:ngRepeat in compiled by $compile does not work,但它并没有解决这里的问题,因为我在范围上手动调用了$digest,但它仍然不起作用。
我指的代码在这里:
js:
angular.module('myApp', []);
angular.module('myApp').directive('test', ['$compile', '$rootScope', '$timeout',
function ($compile, $rootScope, $timeout) {
var myScope = $rootScope.$new();
myScope.numbers = [1, 2, 3];
var html = '<div ng-repeat="number in numbers">{{ number }}</div>';
var html2 = '<div>{{ numbers }}</div>';
var returnObject = $compile(html)(myScope);
$timeout(function () {
myScope.$digest();
console.log(returnObject);
});
return {};
}
]);
html:
<html ng-app="myApp">
<head>
...
</head>
<body>
<h1 test>Hello Plunker!</h1>
</body>
</html>
链接到 plunker:http://plnkr.co/edit/RC1EILu16GPr0MGmSpcb?p=preview
如果您将html 变量切换为html2,它将按预期插入并返回对象。但在目前的状态下,我希望得到一组具有 1、2、3 的 DIV ...
【问题讨论】:
标签: javascript angularjs compilation ng-repeat