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

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -