实现 angular promise if 语句为真,如果不为真则提供默认值

Implement angular promise if statement is true, if not provide a default value

如果用户已登录,我希望实现 Angular 承诺,但如果没有,我希望向后端提供默认值。这目前有效,但显然它是非常重复的。任何人都可以提供任何指导吗?

到目前为止,这是我的代码:

this.getUserDetails = TestService.get({ id: signedIn.id });

this.getUserDetails returns 一个 JSON 对象,然后我需要使用它来发出后续的 GET 请求,但前提是用户已登录。

 // this is executed if user not signed in
if(!signedIn.Id){
 controller.test_type = "Whatever"

   TestService.query({test_type: controller.test_type}, function (prices) {
       this.test = prices.prices;
    }).$promise; 
}

//executed if user is signed in, but must wait for backend to return data first

else{
    this.getUserDetails.$promise.then(function(data){ 
    TestService.query({test_type: data.test_type}, function (prices) {
        this.test = prices.prices;
    }).$promise; 
}

如果需要任何进一步的详细信息,请告诉我。

谢谢

像这样链接和嵌套调用怎么样:

function check(){
  if(signedIn){
    return this.getUserDetails.$promise
  }else{
    return $q.when(controller)  
  }
};

check().then(function(data){
    TestService.query({test_type: data.test_type}, function (prices) {
        this.test = prices.prices;
    });
});