Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何在html代码中为此编写ng repeat for(var i=0;i_Angularjs - Fatal编程技术网

Angularjs 如何在html代码中为此编写ng repeat for(var i=0;i

Angularjs 如何在html代码中为此编写ng repeat for(var i=0;i,angularjs,Angularjs,您不需要编写围绕全局变量的循环。您只需保留变量本身,然后调用循环。稍后,您只需在html代码中使用全局变量即可 我制作了一个很酷的片段,让您了解它的工作原理: angular.module('demo',[]) .controller('Ctrl',['$scope',函数($scope){ $scope.inputs=[]; 变量a=['name1','name2','name3']; var b=[133233456]; //此代码必须在其他地方调用。它可能是函数的一部分。 对于(变量i=

您不需要编写围绕全局变量的循环。您只需保留变量本身,然后调用循环。稍后,您只需在html代码中使用全局变量即可

我制作了一个很酷的片段,让您了解它的工作原理:

angular.module('demo',[])
.controller('Ctrl',['$scope',函数($scope){
$scope.inputs=[];
变量a=['name1','name2','name3'];
var b=[133233456];
//此代码必须在其他地方调用。它可能是函数的一部分。
对于(变量i=0;i

{{输入| json}
您的JS无效,将生成长度为1的数组。请将其替换为:

$scope.inputs=[];
for(var i=0;i<a.length;i++){// be sure that a.length >=b.length
   $scope.inputs.push({name:a[i],value:b[i]}); // push will add new entry to your inputs array.
}
$scope.inputs=[];
对于(变量i=0;i=b.length
$scope.inputs.push({name:a[i],value:b[i]});//push将向输入数组添加新条目。
}
您可以在ng repeat中使用它:

<div ng-repeat="entry in inputs"> {{entry.name}} : {{entry.value}} </div>
{{entry.name}:{{entry.value}

如果控制器中有一个数组,其作用域在html中可见

angular.module('appName').controller('mainCtrl', mainCtrl);

function mainCtrl($scope) {
    $scope.inputs = [
        key: value,
        ...
    ];
}
在html中,您可以在控制器范围内使用
ng repeat
。您可以在多个不同的html标记上使用
ng repeat
指令,例如
列表、a
div
、选择下拉列表等

<div ng-controller="mainCtrl">
     <ul>
          <li ng-repeat="item in inputs">{{item.key}} <!-- Prints 'value' --></li>
     </ul>
</div>

  • {{item.key}

我们用html编写ng repeat
<div ng-controller="mainCtrl">
     <ul>
          <li ng-repeat="item in inputs">{{item.key}} <!-- Prints 'value' --></li>
     </ul>
</div>