Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何重写我的typescript代码,使其不会触发http请求?_Angular_Typescript_Rxjs - Fatal编程技术网

Angular 如何重写我的typescript代码,使其不会触发http请求?

Angular 如何重写我的typescript代码,使其不会触发http请求?,angular,typescript,rxjs,Angular,Typescript,Rxjs,嗨,我有一个有棱角的UI。我有一个名为range的formcontrol。范围基本上是两个矩对象的数组(类似于[矩(),矩()])。当用户从UI中单击清除按钮时,范围被清除并设置为null。当范围为null时,我不想触发http调用(ActivityService.GetUnpaginatedActivationSwithinRange)。我该怎么做。以下是我的代码段。换句话说,如果this.rangeControl.value[0]或this.rangeControl.value[1]或“va

嗨,我有一个有棱角的UI。我有一个名为range的formcontrol。范围基本上是两个矩对象的数组(类似于[矩(),矩()])。当用户从UI中单击清除按钮时,范围被清除并设置为null。当范围为null时,我不想触发http调用(ActivityService.GetUnpaginatedActivationSwithinRange)。我该怎么做。以下是我的代码段。换句话说,如果this.rangeControl.value[0]或this.rangeControl.value[1]或“value”等于null,我不想触发调用this.campaitsservice.getUnpaginatedCampaignsWithinRange方法

 if(!this.isGlobalAdmin){
  this.rangeControl.valueChanges.pipe(
    switchMap( value =>  this.campaignsService.getUnpaginatedCampaignsWithinRange(
      { 
        accountIds : this.currentUserAccount? this.currentUserAccount.id: null,
        campaignStatuses: this.campaignStatusIds,
        rangeStartDate: (this.rangeControl.value[0]).toISOString(),
        rangeEndDate: (this.rangeControl.value[1]).toISOString()
      }
    )
  ),
  tap((campaigns) => { this.campaigns = campaigns; })
  ).subscribe();
}
非常感谢您的帮助


谢谢您

简单,只需过滤您的值更改。根据您的要求添加过滤和传递条件

RxJS过滤器用于根据给定谓词过滤源可观测数据发出的值。如果条件返回true,过滤器将发出源可观测数据发出的值,否则不会

过滤器(x=>x!==null)

有关RxjS过滤器,请参阅以下内容:

if(!this.isGlobalAdmin){
  this.rangeControl.valueChanges.pipe(
    filter(x => x !== null), // add filter and pass condition as per your requirement
    switchMap( value =>  this.campaignsService.getUnpaginatedCampaignsWithinRange(
      { 
        accountIds : this.currentUserAccount? this.currentUserAccount.id: null,
        campaignStatuses: this.campaignStatusIds,
        rangeStartDate: (this.rangeControl.value[0]).toISOString(),
        rangeEndDate: (this.rangeControl.value[1]).toISOString()
      }
    )
  ),
  tap((campaigns) => { this.campaigns = campaigns; })
  ).subscribe();
}