我尝试创建一个输入数字,提供循环span
代码like
<div ng-app='myApp' ng-controller="myCtrl">
<input type="number" ng-model="number">
<li ng-repeat="n in [] | range:{{number}}"><span class="glyphicon glyphicon-star" >{{n}}</span></li>
</div>
这是我的JS
var app = angular.module('myApp', []);
function myCtrl($scope){
}
app.filter('range', function() {
return function(val, range) {
range = parseInt(range);
for (var i=0; i<range; i++)
val.push(i);
return val;
};
});
但那不起作用。如何做到这一点,谢谢
答案 0 :(得分:5)
您不必在{{number}}
指令中插入ng-repeat
,您可以直接使用它。
变化:
<div ng-app='myApp' ng-controller="myCtrl">
<input type="number" ng-model="number">
<li ng-repeat="n in [] | range:{{number}}"><span class="glyphicon glyphicon-star" >{{n}}</span></li>
</div>
为:
<div ng-app='myApp' ng-controller="myCtrl">
<input type="number" ng-model="number">
<li ng-repeat="n in [] | range:number"><span class="glyphicon glyphicon-star" >{{n}}</span></li>
</div>
答案 1 :(得分:1)
将表达式{{number}}
替换为number
中的模型range filter
。
更新的代码:
<div ng-app='myApp' ng-controller="myCtrl">
<input type="number" ng-model="number">
<li ng-repeat="n in [] | range: number"><span class="glyphicon glyphicon-star" >{{n}}</span></li>
</div>