将已解决的承诺注入服务

编程入门 行业动态 更新时间:2024-10-27 05:26:38
本文介绍了将已解决的承诺注入服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我需要从服务器获取一些信息(架构),然后才能设置一系列依赖于该信息的服务.

I need to get some information (a schema) from the server before I set up a bunch of services that depend on that information.

我的服务器提供了一个模式来定义模型的各种属性.在我的 angular 代码中,我有一个获取此架构的服务:

My server provides a schema that defines various properties of a model. In my angular code, I have a service that gets this schema:

services.factory('schema', function($q, $http) {
    var deferred = $q.defer();
        $http.get('schema/').then(function(response) {
        schema = // some function of response.data
        deferred.resolve(schema);
    }, function() {
        deferred.reject('There was a problem fetching the schema');
    }); 
        return deferred.promise;
});

我想将架构对象而不是承诺注入到依赖架构的其他服务中.$routeProvider 允许我们为控制器执行此操作:

I would like to inject the schema object, and not the promise, into other services that depend on the schema. $routeProvider lets us do this for controllers:

app.config(function($routeProvider) {
    $routeProvider.
        when('/', {
            controller: 'SomeCtrl',
            resolve: {
                schema: 'schema'
            },
            ...
        });
});

这允许我像这样定义 SomeCtrl:

and this allows me to define SomeCtrl like this:

controllers.controller('SomeCtrl', function($scope, schema) {
    // schema is an object
    ...
});

但是对于服务,我必须这样做:

But for services, I have to do:

services.factory('SomeService', function(schema) {
    // schema is a promise
    schema.then(function(schema) {
        ...
    });
});

有什么办法可以做到这一点吗?

Is there any way I can do this?

推荐答案

你想要的是延迟引导.已经有一个为此目的编写的插件 - https://github/philippd/angular-deferred-引导程序.

What you want is deferred bootstrap. There is already a plugin written for this purpose - https://github/philippd/angular-deferred-bootstrap.

我在 plunkr 创建了一个示例 - http://plnkr.co/edit/VfISHNULXRLe52NeAsn1?p=预览

I create an example at plunkr - http://plnkr.co/edit/VfISHNULXRLe52NeAsn1?p=preview

*您必须用延迟引导程序替换现有的 ng-app

*You must replace existing ng-app with deferred bootstrap

代码片段 -

angular.element(document).ready(function() {
    deferredBootstrapper.bootstrap({
        element: document.body,
        module: 'plunker',
        resolve: {
            schema: ['$http',
                function($http) {
                    return $http.get('schema.json');
                }
            ]
        }
    });
});

然后,您可以在控制器、服务或工厂中以任何方式使用架构,就像路由解析一样.

Then, you can use schema anyway in controller, services or factory just like route resolve.

工厂示例代码

app.factory('SomeService', function(schema){
    return {
        getTitle: function() {
            return schema.title;
        }
    }
});

这篇关于将已解决的承诺注入服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-21 10:05:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1000415.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:将已

发布评论

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

>www.elefans.com

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