【问题标题】:Passing a Promise from one Service Call To Another in Angular在 Angular 中将 Promise 从一个服务调用传递到另一个服务调用
【发布时间】:2017-05-06 01:48:22
【问题描述】:

所以我有一个服务调用到后端并保存一个对象,该调用设置了一个返回数字的承诺。

这个调用看起来像这样

    saveTcTemplate(item: ITermsConditionsTemplate): ng.IPromise<number> {
        item.modifiedDate = new Date();
        return this.$http.post(this.api + '/SaveTcTemplate', item)
            .then(this.returnData); 
    }

    returnData = (response: any) => {
        return response.data;
    };

这是在创建新对象时,将所有字段设置为所需的值,传递以保存,然后调用以显示。

这是用于在对象保存后拉取对象的 get 函数。

    getTermsConditions(id: number): ng.IPromise<ITermsConditionsTemplate> {
        return this.$http.get(this.api + '/GetTermsConditions',
            {
                params: {
                    id: id
                }
            }).then(this.returnData);
    }

这是对象的初始构造、保存和获取

                this.newTemplate.copyId = 0;
                this.newTemplate.id = 0;
                this.newTemplate.isLibrary = true;
                this.newTemplate.studyFacilityScheduleId = this.studyFacilityScheduleId;
                this.studyTermsConditionsService.saveTcTemplate(this.newTemplate)
                    .then(this.studyTermsConditionsService.getTermsConditions)
                    .then(this.setTemplateData);

当这样设置时,我可以成功保存一个新项目,并将其 id(ng.IPromise 部分)返回给我,并传递给我的 Get 服务调用。

问题是,当以这种方式设置时,this.$http.get 返回未定义。从我认为我从其他类似的堆栈溢出问题中了解到的情况来看,它正在发生,因为我在调用该函数时没有明确地将任何内容传递给它的参数

.then(this.studyTermsConditionsService.getTermsConditions)

为了测试这一点,我还设置了保存并得到这样的结果

                var data: any;
                data = this.studyTermsConditionsService.saveTcTemplate(this.newTemplate);
                this.studyTermsConditionsService.getTermsConditions(data)
                    .then(this.setTemplateData);

这确实奏效了。 $http.Get 被识别,并且可以使用,但是这个设置的问题是;由于 ng.IPromise 的异步特性,数据不会作为承诺的数值发送到我的 Get 函数。它以 $$promise 类型发送,这导致我的 get 函数中的参数为 NaN。

所以我传递一个可用值的一种方式,即 32,但我没有明确传递这个值,所以 $http.get 是未定义的。

另一种方式,我显式传递了一个参数,因此 $http.get 被识别并且可用,但是显式调用的参数是 $$promise 类型,而不是数字类型,不幸的是,ng.IPromise 似乎 在我调用 Get 函数时未解决。

我该如何从这里开始??

【问题讨论】:

    标签: javascript angularjs asynchronous typescript


    【解决方案1】:

    我猜你搞砸了this。当您直接将函数引用作为参数传递时,它会随上下文丢失。您应该像下面这样明确地调用它们。

    代码

    this.newTemplate.copyId = 0;
    this.newTemplate.id = 0;
    this.newTemplate.isLibrary = true;
    this.newTemplate.studyFacilityScheduleId = this.studyFacilityScheduleId;
    this.studyTermsConditionsService.saveTcTemplate((response) => this.newTemplate(response))
        .then((response) => this.studyTermsConditionsService.getTermsConditions(response))
        .then((response) => this.setTemplateData(response));
    

    否则,您可以让您的function 使用arrow function,这将有助于在函数内部使用this 上下文。

    saveTcTemplate(item: ITermsConditionsTemplate): ng.IPromise<number> { .. }
    

    应该是

    saveTcTemplate = (item: ITermsConditionsTemplate): ng.IPromise<number> => { .. }
    

    getTermsConditions(id: number): ng.IPromise<ITermsConditionsTemplate> { .. }
    

    应该是

    getTermsConditions = (id: number): ng.IPromise<ITermsConditionsTemplate> => { .. }
    

    【讨论】:

    • Utlizing .then((response) => this.studyTermsConditionsService.getTermsConditions(response)) 解决了这个问题!谢谢!这里究竟发生了什么可以解决问题?通过添加 (response) => function(response) 我实际上是在告诉 Angular 在响应完成之前不要处理下一个函数?
    • @Wikster2014 no.. 它与 Angular 无关,它与 ES6 有关,通过具有 Arrow 功能,您可以确保 this 将成为当前控制器 this(context) 而执行功能。早先的this 在执行回调函数时丢失了..
    • @Wikster2014 您是否尝试过其他建议?这也应该有效
    • 我尝试按照您第二个建议中的建议将箭头函数添加到方法中,但这会导致错误,并使我的方法无法访问。不知道为什么,但错误说,“检测到无法访问的代码。找不到(functionName)”但是你的第一个建议非常有效!
    猜你喜欢
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    • 2011-01-31
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多