AngularJS ngRepeat不适用于API中的对象

时间:2015-03-12 03:10:57

标签: angularjs angularjs-ng-repeat

我正在尝试显示这个对象,就这么简单...但是当我重新加载页面时,没有任何事情发生,空白,对象是完美的,因为在控制台中...

var store = angular.module('storeApp', []);

store.controller('CategoriesNProducts', ['$scope', function($scope) {
    $scope.moltin = new Moltin({publicId: 'wA7eKb9jzqDvNI1V0HKIeGl9osh3FWcAKzOtZfcj'});

    $scope.get_categories = function() {
        $scope.moltin.Authenticate(function () {
            $scope.moltin.Category.Tree({status: '1'}, function (tree) {
                return tree;
            });
        });
    };

    $scope.categories = $scope.get_categories();
}]);

查看

<div ng-app="storeApp">
    <h1>Store</h1>
    <section id="categories" ng-controller="CategoriesNProducts">
        {{categories.length}}
        <div ng-repeat="(key, value) in categories">
            <div id="{{value.description}}"></div>
            {{ value }}
        </div>
  </section>
</div>

1 个答案:

答案 0 :(得分:4)

确定您的Category.Tree函数是异步的,因此,您无法分配“异步数据”并期望它可以正常分配。您需要在异步函数回调中指定$scope.categories

应该是

 $scope.get_categories = function() {
        $scope.moltin.Authenticate(function () {
            $scope.moltin.Category.Tree({status: '1'}, function (tree) {
               $scope.categories  = tree;
               $scope.apply(); // Use this if Category.Tree came from external lib
              //Using apply, you tell to angularjs that $scope have been change by external lib
            });
        });
    };

 $scope.get_categories();