2016-03-12 214 views

回答

0
  • 陣列項目可以通過方形符號(訪問:

    $Item = [{item1: 'lorem ipsum'}, {item2: [{inner_item:'lorem ipsum', inner_item2: 'lorem ipsum'}]},...] 
    

    我如何通過jQuery中指出像做到這一點在控制器例如:item[0]

  • 對象參數可以通過訪問點符號(例如:item1.paramater

var app = angular.module('app', []).controller('indexCtrl', function ($scope) { 
 
    $scope.item = [ 
 
     {item1: 'lorem ipsum'}, 
 
     {item2: [{inner_item:'lorem ipsum', inner_item2: 'lorem ipsum'}] 
 
     }]; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 
<div ng-app="app" ng-controller="indexCtrl"> 
 
    <p>item[0]: {{item[0]}}</p> 
 
    <p>item[0].item1: {{item[0].item1}}</p> 
 
    <p>item[1]: {{item[1]}}</p> 
 
    <p>item[1].item2[0]: {{item[1].item2[0]}}</p> 
 
    <p>item[1].item2[0].inner_item: {{item[1].item2[0].inner_item}}</p> 
 
</div>

+0

作品對我感謝分配! –

1

你的數據是有點曖昧,但我會告訴你怎麼做兩種變化適當角度的方式:

var app = angular.module('myApp', []); 
 

 
app.controller('TestCtrl', ['$scope', function($scope) { 
 
    $scope.items = [{country: 'Germany'}, 
 
        {country: 'Spain'}, 
 
        {country: 'England'}]; 
 
    $scope.nestedItems = [{continent: 'Europe', 
 
         countries: [ 
 
          {country: 'Germany'}, 
 
          {country: 'Spain'}, 
 
          {country: 'England'}] 
 
         }]; 
 
}]);
.single { 
 
    background-color: lightgreen; 
 
} 
 

 
.nested { 
 
    background-color: lightblue; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"> 
 

 
</script> 
 
<section ng-app="myApp" ng-controller="TestCtrl"> 
 
    <div class="single"> 
 
    <ul> 
 
    <li ng-repeat="item in items"> 
 
     Country: {{item.country}} 
 
    </li> 
 
    </ul> 
 
    </div> 
 
    <div class="nested" ng-repeat="item in nestedItems"> 
 
    <h2>Continent: {{item.continent}}</h2> 
 
    <ul> 
 
    <li ng-repeat="item in item.countries"> 
 
     Country: {{item.country}} 
 
    </li> 
 
    </ul> 
 
    </div> 
 
    
 
    
 
</section>