Javascript 在数组上重复的Angularjs指令

Javascript 在数组上重复的Angularjs指令,javascript,html,angularjs,angularjs-directive,Javascript,Html,Angularjs,Angularjs Directive,我已设置以下指令: app.directive('starRating', function () { return { restrict: 'EA', scope: { rating: '=rating' }, template: '<ul class=\'list-unstyled\'>' + '<li><spa

我已设置以下指令:

app.directive('starRating', function () {
    return {
        restrict: 'EA',
        scope: {
            rating: '=rating'
        },
        template:
            '<ul class=\'list-unstyled\'>' +
                '<li><span class=\'glypicon glyphicon-star\'></span></li>' +
            '</ul>'
    };
});
app.directive('starting',function(){
返回{
限制:“EA”,
范围:{
评级:'=评级'
},
模板:
“
    ”+ “
  • ”+ “
” }; });
然后我有以下HTML:

评级
是一个数组:
[1,3,2,4,5]
这意味着第一个评级是1星,第二个评级是3星,等等


该指令的目标是重复评级图标的数量。

您可以使用for循环来连接模板中“ul”内的“li”元素。这比使用ng repeat更便宜。您可能根本不需要使用列表(ul和li)

此外,您的范围可以是:

 scope: {
        rating: '='
    },

我引用这个答案是为了在不使用指令的情况下完成需求:指令在这里可能很有用,因为它可能会通过他的应用程序重复很多次,这个指令可以帮助您