AngularJS,Async-ng-repeat不等待范围设置

时间:2014-12-01 06:46:54

标签: javascript ajax angularjs asynchronous angularjs-ng-repeat

当我加载索引页面时,我的ajax调用有效并且范围确实已设置。但似乎在使用"产品"。

设置范围之前加载ng-repeat

我觉得应该有办法

  1. 加载页面而不必等待设置范围,但是当它设置好后 - 然后重新运行循环" ng-repeat"
  2. 在初始化循环之前等待AJAX​​调用完成(' ng-repeat')
  3. 干杯!

    products.controller.js

    dvpModule.controller("ProductsController", function ($scope, productRepository) {
        productRepository.get().then(function(products) { 
        $scope.products = products; 
        });
    });
    

    产品-repository.js

    dvpModule.factory('productRepository', function($http, $q) {
    return {
        get: function() {
            var deferred = $q.defer();
    
            $http({
                method: 'POST',
                url: 'http://localhost/test/wp-admin/admin-ajax.php',
                params: { action: 'getAllProductUrls' },
            }).success(deferred.resolve).error(deferred.reject);
            return deferred.promise;
        }
    };
    });
    

    的index.html

    <html ng-app="dvpModule">
    
    <head>
        <script type='text/javascript' src='js/angular.min.js'></script>
        <script src="js/scripts/dvp_module.js" /></script>
        <script src="js/scripts/products-controller.js" /></script>
        <script src="js/scripts/products-repository.js" /></script>
        <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
        <title>DVP {{2 + 2}}</title>
    </head>
    
    <body ng-controller="ProductsController">
        <div>
        <tr ng-repeat="product in products">
            <th>{{product.Category}}</th>
            <th>{{product.Features}}</th>
            <th>{{product.MODEL_SERIES}}</th>
        </tr>
        </div>
    

0 个答案:

没有答案