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.
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
};
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다