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   }; }); 

demo: http://plnkr.co/edit/t6yrnfnhcrq1x2hcmxjb?p=preview


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -