Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I try to create a input number that provided total number for loop my span tag 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>

And here is my 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;
  };
});

But that's not working. How to do that thanks

share|improve this question

2 Answers 2

up vote 3 down vote accepted

You don't have to interpolate {{number}} inside the ng-repeat directive, you can directly use it as it is.

change:

<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>

to:

<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>
share|improve this answer

Replace the expression {{number}} with the model number in the range filter. Updated code:

<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>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.