2017-04-08 99 views
0

上我有以下input type="number"角NG-重复工作不元素

<input type="number" 
ng-model="question.numberOfLines" min="1" max="5" /> 

我登录控制台上的NG-模型,它给了我一个号码,所以我知道它的工作。现在我想重复一个<hr />元素的次数等于我之前提到的变量,但它不起作用。这是我想要做的:

<hr class="question-line" ng-repeat="lines in question.numberOfLines" /> 

任何想法我做错了什么?

+0

工作你怎么能这样在一个数字上做'ng-repeat'? –

回答

0

您只能使用ng-repeat的集合。您正尝试使用整数。

文档:https://docs.angularjs.org/api/ng/directive/ngRepeat

在这种情况下,您可以创建一个new Array(number)和遍历这个。

里面你控制器

$scope.myArrayNumber = new Array($scope.question.numberOfLines); 

而且你查看内:

<hr class="question-line" ng-repeat="lines in myArrayNumber " /> 
0

您没有使用NG-重复正确。

ng-repeat正在等待一个数组。

比方说,我们有这样的代码:

angular.module('app',[]) 
.controller('sample', ['$scope', function($scope) { 
    $scope.question = { 
    lines: [1,2,3], 
    numberOfLines: 3, 
    }; 
}]); 

则必须使用NG-重复如下:

<hr class="question-line" ng-repeat="line in question.lines" /> 

你可以看到它在Plunker