javascript - How does ServiceNameQuery work in AngularJS -
for example, there following piece of code:
$scope.posts = post.query({ user_id: $stateparams.user_id })
how construction work? when js execute line posts may empty, after time server side return posts , view render lot of posts. how 'query' function may return value in future? don't understand it, because ajax call asynchronous, , don't send value in function. please, describe moment. thanks.
i know promises , $q, don't understand how works here.
internally executes asynchrnous operation , returns reference empty array.
when asynchronous operation has finished populate array elements.
$scope.posts
reference same array, containing elements.
simple example implementation:
app.factory('post', function ($timeout) { var query = function (selector) { var result = []; // asynchronous operation $timeout(function () { (var = 0; < 5; i++) { result.push({ id: }); } }, 3000); return result; }; return { query: query }; });
Comments
Post a Comment