Javascript $compile和ng repeat未返回正确的元素

Javascript $compile和ng repeat未返回正确的元素,javascript,angularjs,compilation,ng-repeat,Javascript,Angularjs,Compilation,Ng Repeat,我无法使$compile手动使用ng repeat。这里有一个类似的问题:,但它并没有解决这里的问题,因为我手动调用了作用域上的$digest,但它仍然不起作用 我所指的代码如下: js: angular.module('myApp', []); angular.module('myApp').directive('test', ['$compile', '$rootScope', '$timeout', function ($compile, $rootScope, $timeou

我无法使
$compile
手动使用
ng repeat
。这里有一个类似的问题:,但它并没有解决这里的问题,因为我手动调用了作用域上的
$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 ng-app="myApp">

    <head>
    ...
    </head>

    <body>
        <h1 test>Hello Plunker!</h1>
    </body>

</html>
angular.module('myApp',[]);
angular.module('myApp').directive('test',['$compile','$rootScope','$timeout',',
函数($compile、$rootScope、$timeout){
var myScope=$rootScope.$new();
myScope.numbers=[1,2,3];
var html='{number}}';
var html2='{numbers}}';
var returnObject=$compile(html)(myScope);
$timeout(函数(){
myScope.$digest();
console.log(returnObject);
});
返回{};
}
]);
html

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 ng-app="myApp">

    <head>
    ...
    </head>

    <body>
        <h1 test>Hello Plunker!</h1>
    </body>

</html>

...
你好,普朗克!
链接到plunker


如果将
html
变量切换为
html2
,它将按预期插入并返回对象。但是在它的当前状态下,我希望得到一个具有1、2、3…的div集合。

在内部,ng repeat通过向调用ng repeat指令的父元素添加元素来工作。已编译的ng repeat没有可添加其元素的父级,因此无法工作

添加父div()

var html='{{number}}';
var html2='{numbers}}';
var returnObject=$compile(html)(myScope);
$timeout(函数(){
myScope.$digest();
log(returnObject.children());