I know it works in this way but I don't know why it works in this way. Please see the code below. So, why we use "."
to access an object in HTML part and use "[]"
to access an object in JavaScript part? Thanks.
<div ng-app="testApp" ng-controller="testCtrl">
<div ng-repeat="detail in details">
<span>{{detail.numbers.length}}</span> <!-- Output: 1, 2, 3 -->
<!-- Won't work: <span>{{details[detail].numbers.length}}</span> -->
</div>
</div>
angular.module("testApp", []).controller("testCtrl", function ($scope) {
$scope.details = [{"numbers": [1]}, {"numbers": [2, 2]}, {"numbers": [3, 3, 3]}];
for (var detail in $scope.details) {
console.log($scope.details[detail].numbers.length); //Output: 1, 2, 3
//Won't work: console.log(detail.numbers.length);
}
})
JSFiddle: https://jsfiddle.net/ealonwang/d5yL8ydk/18/.
Right click and inspect the page to see console.log()
results.