Javascript-форум (https://javascript.ru/forum/)
-   Angular.js (https://javascript.ru/forum/angular/)
-   -   angularJS + json file + object + .length (https://javascript.ru/forum/angular/47384-angularjs-json-file-object-length.html)

klierik 21.05.2014 18:48

angularJS + json file + object + .length
 
Здравствуйте.

Не могу понять почему ангуляр не считает кол-во чилдов в объекте.
Код html:

<table class="table" ng-controller="ShowIndexStatistics as stats">
    <tbody ng-repeat="indexSection in indexData">

        <tr ng-repeat="account in indexSection.details.accounts">

            <th ng-if="$first" ng-attr-rowspan="{{indexSection.details.accounts}}">
                {{indexSection.name}} <small>({{indexSection.details.symbol}})</small>
                <p>{{indexSection.details.accounts.length}}</p>

                <pre>{{indexSection.details.accounts.length}}</pre>
            </th>

            <td>{{account.name}} <small>({{account.id}})</small></td>
        </tr>
    </tbody>
</table>


в контроллере:
var dbPath = "../../db/collection.json";

    $scope.indexData = null;

    $http({method: 'GET', url: dbPath}).
        success(function (data, status, headers, config) {
            // this callback will be called asynchronously
            // when the response is available

            $scope.indexData = data;
        }).
        error(function (data, status, headers, config) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
        });


В итоге строка:
<pre>{{indexSection.details.accounts.length}}</pre>

Возвращает пустоту.

Скажите, пожалуйста, что я делаю не так?

Tek 22.05.2014 12:11

Попробуйте обернуть $scope.indexData = data; в $scope.applay

nerv_ 22.05.2014 12:28

Цитата:

Сообщение от klierik
Не могу понять почему ангуляр не считает кол-во чилдов в объекте

потому, что у объекта нет свойства length, в отличии от массива

klierik 26.05.2014 10:44

Цитата:

Сообщение от nerv_ (Сообщение 312845)
потому, что у объекта нет свойства length, в отличии от массива

Совершенно верно.
Как оказалось для того что бы реализовать задачу, потребовалось изменить синтаксис в JSON файле.
Данный положил в {}
А вот перечень данных лежит в []

В итоге .length работает как и ожидается.


Часовой пояс GMT +3, время: 07:05.