AngularJS ng-options create range

后端 未结 8 898
鱼传尺愫
鱼传尺愫 2020-11-28 09:58

I am trying to create a select element that has a list of numbers 1 to pages where pages is a variable that is the number of pages I have. What i don\'t know how to do is to

8条回答
  •  天命终不由人
    2020-11-28 10:22

    Piggybacking on martynas' answer. I modified it to include the last value in the range:

    /*
     * Creates a range
     * Usage example: 
     */
    
    .filter('range', function () {
        return function (input, start, end) {
            var direction;
    
            start = parseInt(start);
            end = parseInt(end);
    
            if (start === end) { return [start]; }
    
            direction = (start <= end) ? 1 : -1;
    
            while (start != end) {
                input.push(start);
    
                if (direction < 0 && start === end + 1) {
                    input.push(end);
                }
    
                if (direction > 0 && start === end - 1) {
                    input.push(end);
                }
    
                start += direction;
            }
    
            return input;
        };
    });
    

提交回复
热议问题