为了初始化服务等待服务器数据

编程入门 行业动态 更新时间:2024-10-28 02:33:08
本文介绍了为了初始化服务等待服务器数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想创建时,应用程序加载首次从服务器获取数据的角度服务的一次,然后我就可以从本地服务对象查询数据。以下是我试图做到这一点:

I want to create an angular service that when the app is loaded for the first time will fetch data from the server once and then I'll be able to query the data from the local service object. Here's how I tried to do it:

angular.module('my.services').factory('Properties', function ($http) { var properties = $http.get("/properties"); return { get: function (property) { // Logic here console.log(property); } } });

问题是 - $ http.get()是异步的,为了达到我想要做的,我一定要同步使用它。有没有办法与本土AngularJS办呢?或者我必须使用XHR要求呢?

Problem is - $http.get() is asynchronous and in order to achieve what I'm trying to do, I have to use it synchronously. Is there a way to do it with "native" AngularJS? Or I must use an XHR call for this?

推荐答案

您可以尝试做这样的事情。

You can try to do something like this

angular.module('my.services').factory('Properties', function ($http) { var propertiesPromise = $http.get("/properties"); return { get: function (property) { propertiesPromise.then(function(properties){ console.log(properties); }); } } });

更多推荐

为了初始化服务等待服务器数据

本文发布于:2023-08-07 14:26:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319618.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:初始化   服务器   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!