How does ServiceNameQuery work in AngularJS

malcoauri

For example, there is a following piece of code:

$scope.posts = Post.query({ user_id: $stateParams.user_id })

How does this construction work? When JS execute this line posts may be empty, but after some time server side will return some posts and my view will render a lot of posts. But how 'query' function may return value in future? I don't understand it, because AJAX call is asynchronous, and I don't send any value in any function. Please, describe this moment. Thanks.

I know about promises and $q, but I don't understand how it works here.

tasseKATT

Internally it executes an asynchrnous operation and returns a reference to an empty array.

When the asynchronous operation has finished it will populate that array with elements.

$scope.posts is a reference to the same array, containing those elements.

Simple example implementation:

app.factory('Post', function ($timeout) {

  var query = function (selector) {

    var result = [];

    // An asynchronous operation
    $timeout(function () {
      for (var i = 0; i < 5; i++) { 
        result.push({ id: i });
      }
    }, 3000);

    return result;
  };

  return {
    query: query
  };
});

Demo: http://plnkr.co/edit/t6yRNFnHcrQ1X2HCmXjb?p=preview

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Why does this angularjs directive not work?

분류에서Dev

AngularJS: $http does not work to outer API

분류에서Dev

Angularjs ng-view does not work

분류에서Dev

AngularJS Button to reload state does not work

분류에서Dev

Why and how does this work?

분류에서Dev

How does this work in javascript

분류에서Dev

endturnwithnextparticipants, how does it work?

분류에서Dev

How does SimpleCORSFilter work?

분류에서Dev

Phonegap how does it work?

분류에서Dev

How does collect static work?

분류에서Dev

How does "newElements" work in Javascript?

분류에서Dev

How does scanr work? Haskell

분류에서Dev

how does readyRead() work in Qt?

분류에서Dev

How does this pointer typecasting work?

분류에서Dev

How does shared hosting work?

분류에서Dev

How does the INTERVAL datatype work?

분류에서Dev

How does file compression work?

분류에서Dev

How does Windows API work?

분류에서Dev

How does -Infinity actually work?

분류에서Dev

how does int*& variableName work ?

분류에서Dev

How does 'telnet localhost' work?

분류에서Dev

How does return work for a function local variable?

분류에서Dev

How does ForkJoinPool#awaitQuiescence actually work?

분류에서Dev

How does * to access pointers of variables or arrays work?

분류에서Dev

How does Xojo UI rendering for desktop work?

분류에서Dev

How does this implementation of array intersection work?

분류에서Dev

How does Gradient.Evaluate work internally?

분류에서Dev

How Does Initalizing a Javascript Modal Work in Materializecss?

분류에서Dev

How does Eclipse web service explorer work?