我需要动态编译 html 并将其作为文本从函数传递。所以,我有这个代码(用于调试目的的简化版本):
angular.module('app', [])
.run(function($rootScope, $compile){
var data = ["1","2"];
var html =
'<div>' +
'<ul>' +
'<li ng-repeat="score in data">{{score}}</li>' +
'</ul>'+
'</div>';
var el = angular.element(html);
$rootScope.data = data;
var result = $compile(el)($rootScope);
console.log(result.html());
})
Run Code Online (Sandbox Code Playgroud)
结果只是:
<ul><!-- ngRepeat: score in data --></ul>
Run Code Online (Sandbox Code Playgroud)
所以,看起来 ngRepeat 不会“重复”“li”元素。
为什么?
JsFiddle: http: //jsfiddle.net/yoorek/K4Cmk/
(我知道 DOM 操作应该在指令等中,并且我知道如何以其他方式进行操作,但我需要理解为什么这不起作用)
angularjs ×1