Javascript 循环使用Angularjs中的ng选项

Javascript 循环使用Angularjs中的ng选项,javascript,angularjs,Javascript,Angularjs,我试图使用Angularjs中的ng选项在选择框中推送一个值数组 把手: <div class="col-md-8"> <select ng-options="item as item.label for item in items track by item.id" ng-model="selected"> <option></option> </select> </div> v

我试图使用Angularjs中的ng选项在选择框中推送一个值数组

把手:

<div class="col-md-8">
    <select ng-options="item as item.label for item in items track by 
    item.id" ng-model="selected">
        <option></option>
    </select>
</div>
var app = angular.module('myapp', []);

app.controller('mycontroller', function($scope) {

$scope.arealistArray=[];
$scope.items=[]; 

for(j=1;j<3;j++){
    $scope.arealistArray.push([{id: 'id'+j,label: 'aLabel'+j,subItem: { 
    name: 'aSubItem'+j }}]);
} 

$scope.items = $scope.arealistArray;

控制器:

<div class="col-md-8">
    <select ng-options="item as item.label for item in items track by 
    item.id" ng-model="selected">
        <option></option>
    </select>
</div>
var app = angular.module('myapp', []);

app.controller('mycontroller', function($scope) {

$scope.arealistArray=[];
$scope.items=[]; 

for(j=1;j<3;j++){
    $scope.arealistArray.push([{id: 'id'+j,label: 'aLabel'+j,subItem: { 
    name: 'aSubItem'+j }}]);
} 

$scope.items = $scope.arealistArray;
var-app=angular.module('myapp',[]);
app.controller('mycontroller',函数($scope){
$scope.arealistArray=[];
$scope.items=[];

对于(j=1;j您正在另一个数组中推进一个数组。您应该删除“[]”


这是一个基于您的代码的数组。

您正在将一个数组推送到另一个数组中。您应该删除“[]”

$scope.arealistArray.push([{id: 'id'+j,label: 'aLabel'+j,subItem: { 
name: 'aSubItem'+j }}]);
这是一个基于你的代码

$scope.arealistArray.push([{id: 'id'+j,label: 'aLabel'+j,subItem: { 
name: 'aSubItem'+j }}]);
如果与上面类似,则需要指定索引或

 ng-options="item as item.label for item in items[0] track by item.id"
如果是对象,则在推送时移除[]

$scope.arealistArray.push({id: 'id'+j,label: 'aLabel'+j,subItem: { 
name: 'aSubItem'+j }});
var-app=angular.module('myapp',[]);
app.controller('mycontroller',函数($scope){
$scope.arealistArray=[];
$scope.items=[];
对于(j=1;j
如果与上面类似,则需要指定索引或

 ng-options="item as item.label for item in items[0] track by item.id"
如果是对象,则在推送时移除[]

$scope.arealistArray.push({id: 'id'+j,label: 'aLabel'+j,subItem: { 
name: 'aSubItem'+j }});
var-app=angular.module('myapp',[]);
app.controller('mycontroller',函数($scope){
$scope.arealistArray=[];
$scope.items=[];

对于(j=1;ju,不客气。请注意,我已更新了我的答案。经过一些研究,您在代码中使用的
track by
表达式是正确的。不客气。请注意,我已更新了我的答案。经过一些研究,您在代码中使用的
track by
表达式是正确的。