AngularJS, Async-ng-repeat doesn't wait for scope to be set

When I load my index page, my ajax call works and the scope is set. But it looks like the ng-repeat is loaded before the scope is set using "products".

I feel like there must be a way either

  • Load the page without waiting for the region to be set, but when it is set, repeat the "ng-repeat" loop
  • Wait for the AJAX call to complete before initializing the loop ('ng-repeat')

Hooray!

products.controller.js

dvpModule.controller("ProductsController", function ($scope, productRepository) {
    productRepository.get().then(function(products) { 
    $scope.products = 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>

      

+3
javascript angularjs ajax asynchronous angularjs-ng-repeat


source to share


No one has answered this question yet

Check out similar questions:

4523
Thinking in AngularJS if I have a jQuery background?
3952
Setting "checked" for a checkbox with jQuery?
3178
AngularJS: Service vs provider vs factory
1850
What is the scope of variables in JavaScript?
1690
How does data binding work in AngularJS?
1195
How do I access the $ scope variable in the browser console using AngularJS?
1073
How to use $ scope. $ Watch and $ scope. $ Apply in AngularJS?
998
What is the difference between "@" and "=" in scope in AngularJS?
969
What are the nuances of the scope of prototype / prototypal inheritance in AngularJS?
912
'this' vs $ scope in AngularJS controllers



All Articles
Loading...
X
Show
Funny
Dev
Pics