简体   繁体   English

AngularJS ng-repeat x次动态

[英]AngularJS ng-repeat x times dynamic

Preface: 前言:

在此处输入图片说明

                 <input type="tel" ng-model="people" placeholder="6" maxlength="2">
                    </label>
                    <ion-item>
                    {{people}}
                        <i class="ion-man icon-large padding-right" ng-repeat="i in getNumber(people) track by $index"></i>
                    </ion-item>

$scope.getNumber = function(num) {
    return new Array(num);
}

I have an input wherein the user can type in a number. 我有一个输入,用户可以在其中输入数字。 I follow it up with an ng-repeat. 我会继续进行ng-repeat。 Currently the ng-repeat doesn't seem to be bound to the people model. 目前,ng-repeat似乎并不局限于people模型。 How can I bind people to refresh the ng-repeat or perhaps trigger it again on ng-change? 如何约束人们刷新ng-repeat或在ng-change上再次触发它?

To clarify, the ng-repeat code does work --- here is what happens when I replace people with 3 : 澄清一下,ng-repeat代码确实起作用---这是当我用3代替people时发生的情况:

                        <span class="input-label">¿Cuanta gente hay en la mesa?</span>
                        <input type="tel" ng-model="people" placeholder="6" maxlength="2">
                    </label>
                    <ion-item>
                    {{people}}
                        <i class="ion-man icon-large padding-right" ng-repeat="i in getNumber(3) track by $index"></i>
                    </ion-item>

在此处输入图片说明

new Array(num) when num is a string will give you an array with 1 element and is functionally the same as [num] num是字符串时, new Array(num)将为您提供一个包含1个元素的数组,并且在功能上与[num]相同

You need to parseInt the string first so that you have a number. 您需要首先parseInt该字符串,以便您有一个数字。

$scope.getNumber = function(num) {
    return new Array(parseInt(num, 10));
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM