我正在尝试在指令中添加“运行时”和ng-repeat,并且当在html中作为属性放置时,相同的ng-repeat工作,当从指令添加时它不会。请参阅下面的代码和插件。注意:这是事物的一个非常简化的版本,但正如您所看到的,添加了行(我们只有3行),但值显示为空。感谢。
编辑:我简化了示例以消除实验中可能产生的噪音...
获得的结果:
使用ng-repeat作为属性的列表
ITEM:A001
ITEM:A002
ITEM:A003
注入ng-repeat的列表
ITEM:
ITEM:
ITEM:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script type="text/javascript">
angular.module('rlfList', []);
angular.module('rlfList').directive('rlfItem', ['$compile', function ($compile) {
return {
restrict: 'EA',
scope: false,
link: function link(scope, element, attrs) {
if (!element.hasClass('rlfListRow')) {
element.addClass('rlfListRow');
element.attr('ng-repeat', 'item in records');
$compile(element)(scope);
}
}
};
}]);
angular.module('rlfList').controller('rlfController', ['$scope', '$timeout', function ($scope, $timeout) {
$scope.records = [];
$scope.records[0] = { number: 'A001' };
$scope.records[1] = { number: 'A002' };
$scope.records[2] = { number: 'A003' };
}]);
</script>
<div ng-app="rlfList" ng-controller="rlfController">
<div style="margin-top: 20px;">LIST with ng-repeat as attribute</div>
<div ng-repeat="item in records"><span>ITEM : {{ item.number }}</span></div>
<div style="margin-top: 20px;">LIST with ng-repeat injected</div>
<div rlf-item><span>ITEM : {{ item.number }}</span></div>
</div>
答案 0 :(得分:0)