加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 如何在角度服务中使用带有promise模式的restangula

发布时间:2020-12-17 17:49:11 所属栏目:安全 来源:网络整理
导读:我有一个服务与休息角度与以下结构 function countrySvc(restangular) { restangular.addResponseInterceptor(function (data,operation,what,url,response,deferred) { if (operation === 'getList') { var newResponse = response.data; return newRespons
我有一个服务与休息角度与以下结构

function countrySvc(restangular) {
    restangular.addResponseInterceptor(function (data,operation,what,url,response,deferred) {

        if (operation === 'getList') {
            var newResponse = response.data;

            return newResponse;
        }
        return response;
    });
    var baseCountry = restangular.all('country');


    this.countries = function() {

        baseCountry.getList();

    };
}

也是一个控制器

function countryCtrl(scope,countrySvc) {


    scope.countries = countrySvc.countries();

}

但是当我从控制器访问国家时,结果是空的,并且数据成功请求,我的问题是如何从具有正确承诺模式的响应中提取数据,即(当我访问scope.countries时我需要一组国家/地区)

解决方法

你需要解决承诺……

有两种方法可以做到……

1)使用$object

只需添加.$object即可结束承诺,一旦请求完成,它就会解除承诺……

scope.countries = countrySvc.countries().$object;

2)然后使用

如果你需要在承诺解决后需要做一些东西选择这个选项,一旦请求完成回调函数然后将被触发

scope.countries = countrySvc.countries().then(function (response){
    // DO SOMETHING IF YOU NEED BEFORE SET OBJECT
    scope.countries = response;
    // DO SOMETHING IF YOU NEED AFTER SET OBJECT
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读