I understand the functionality, but I'm curious about the reasoning behind using "."
in HTML to access an object and "[]"
in JavaScript. Here is the code snippet for reference. Can anyone shed some light on this? Thank you.
<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/.
To view the results of console.log()
, right click and inspect the page.