繁体   English   中英

什么是正确的方法/可以链接两个AngularJs服务调用,然后对返回的数据执行功能?

[英]What is the correct way/can you chain two AngularJs service calls and then perform a function with the returned data?

我想链接这两个服务调用,并在结果中执行forEach loop以过滤我的数据,但在Chrome中收到TypeError:“ SocialMediaUserService.channelProfiles不是函数”消息。

但是,这在IE中有效,并且没有警告或消息。 :)

function getChannelProfiles() {

    GetUserAccessService.returnBrandProfileID().then(function (brandProfileID) {

        SocialMediaUserService.channelProfiles().then(function (channelProfiles) {

            channelProfiles.forEach(function (channel) {

                if (channel.brand_profile_id === brandProfileID && channel.channel_type === 'facebook') {
                    $scope.facebookChannels.push(channel.channel_url);
                    console.log($scope.facebookChannels);
                }

            });
        });
    });
}

编辑:这是我的SocialMediaUserService.channelProfiles服务调用:

this.channelProfiles = function () {

  var channelProfiles = pullSocialMediaData('list_channel_profiles.json');

  console.log("Channel Profiles Logged: " + channelProfiles);

  return channelProfiles;

}

这是我的SocialMediaUserService.returnBrandProfileID服务调用:

this.returnBrandProfileID = function () {

  var brandProfileID = $q.defer();

  if (angular.isUndefined($sessionStorage.brandProfileID)) {

      GetDataService.getItems('GetUserAccess/' + $cookies.get('authenticationID'))

      .success(function (accessObject) {

          brandProfileID.resolve(accessObject.FusewareID);
      })

      .error(function (error, status) {
          console.error('Fuseware API error: ' + error + ' Status message: ' + status);
      });
  }

  else {
      brandProfileID.resolve($sessionStorage.brandProfileID);
  }

  return brandProfileID.promise;

};

编辑2:这是pullSocialMediaData函数:

function pullSocialMediaData(url) {

  var userData = $q.defer();

  GetFusionDataService.getItems(url)
    .success(function (data) {
        userData.resolve(data);
    })
    .error(function (error, status) {

    });

  return userData.promise;

}

谢谢!

SocialMediaUserService.channelProfiles可能设计如下:

this.channelProfiles = function () {

  var channelProfilesPromise = new Promise(function (resolve, reject){

    pullSocialMediaData('list_channel_profiles.json').then(function(result){

        console.log("Channel Profiles Logged: " + result);
        resolve(result);
    });

  });

    return channelProfilesPromise

};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM