【发布时间】:2025-12-04 02:10:02
【问题描述】:
我试图在ng-repeat 中调用ng-init 函数,它只适用于第一个元素:
<li ng-repeat="comment in ad.comments|limitTo:quantity | orderBy : sortComment : true">
<div id="starsDiv" ng-init="orderComment(comment.stars)"></div>
Comment: {{comment.text}}
<cite class="clearfix"> on {{comment.posted | date}}</cite>
</li>
ng-repeat函数里面的orderComment需要初始化starDiv:
$scope.orderComment= function(numOfStars){
var html = '<i class="fa fa-star-o" style="color:gold;"></i>';
for(var i =0 ; i<numOfStars-1;i++)
{
html += '<i class="fa fa-star-o" style="color:gold;"></i>';
}
document.getElementById("starsDiv").innerHTML = html;
};
通过comment.stars 的值将HTML 注入starsDiv。
例如,如果注释等于 4,则将有 4 个 HTML 元素:
'<i class="fa fa-star-o" style="color:gold;"></i>'
在里面。
【问题讨论】:
-
到底是什么问题?适合我:jsfiddle.net/330kedvq
-
我在 ng-init 中注入 HTML。
-
您不应该对
ng-init这样做。要么将 html 放在 div 中,要么使用自定义指令将comment.stars呈现出来。 -
你可以为此创建一个“jsfiddle”吗?
标签: javascript angularjs angularjs-ng-repeat angularjs-ng-init