Angularjs 将ng repeat绑定到空数组

Angularjs 将ng repeat绑定到空数组,angularjs,Angularjs,有没有更优雅的方法 <input type="text" id="items" ng-model="items[0]" placeholder="item1"> <input type="text" id="items" ng-model="items[1]" placeholder="item2"> <input type="text" id="items" ng-model="items[2]" placeholder="item3"> <input

有没有更优雅的方法

<input type="text" id="items" ng-model="items[0]" placeholder="item1">
<input type="text" id="items" ng-model="items[1]" placeholder="item2">
<input type="text" id="items" ng-model="items[2]" placeholder="item3">
<input type="text" id="items" ng-model="items[3]" placeholder="item4">
<input type="text" id="items" ng-model="items[4]" placeholder="item5">


我想使用
ng repeat
,但是没有要迭代的项,因为
items
一开始是空的。

ng repeat
不会在空数组上迭代。您可以尝试将设置项设置为包含5个空对象的数组,然后稍后更新这些对象。

ng repeat
不会在空数组上迭代。您可以尝试将项目设置为包含5个空对象的数组,然后稍后更新这些对象。

您可能希望在控制器中设置项目,如下所示:

$scope.items = [null, null, null, null, null];

这样,您将有一个要迭代的数组。

您可能希望在控制器中设置如下项:

$scope.items = [null, null, null, null, null];

有了它,您将有一个数组要迭代。

或者您可以在控制器中使用new关键字对其进行不同的初始化

  $scope.items = new Array(5);

或者,您可以在控制器中使用new关键字以不同的方式初始化它

  $scope.items = new Array(5);

您可以使用“按$index跟踪”列出空填充数组:

看中


您可以使用跟踪$index列出空填充数组:

看中



如果它们是空的,为什么要查看它们?也许多一些上下文会更好。您应该初始化控制器或指令中的项[]。你可以创建一个有5个位置的数组,但是你需要把track by$index放进去,这样重复就可以了为什么你想看到它们是空的?也许多一些上下文会更好。您应该初始化控制器或指令中的项[]。你可以创建一个有5个位置的数组,但是你需要把track by$index放进去,这样重复就可以了