Javascript subscribe上未定义角度8函数参数

Javascript subscribe上未定义角度8函数参数,javascript,rxjs,angular8,Javascript,Rxjs,Angular8,我有两个函数,在里面我做http调用并订阅它。 在subscribe内的update函数中,外部函数param对我可用,但在deleteDeviceGroup函数中,groupId参数在subscribe内未定义。 为什么它们之间有区别 update(id, device: Partial<IDevice>) { this.devicesDataService.updateDevice(id, device).subscribe(res => { debug

我有两个函数,在里面我做http调用并订阅它。 在subscribe内的update函数中,外部函数param对我可用,但在deleteDeviceGroup函数中,groupId参数在subscribe内未定义。 为什么它们之间有区别

update(id, device: Partial<IDevice>) {
    this.devicesDataService.updateDevice(id, device).subscribe(res => {
      debugger;
      if (res['status'] && res['status'] === 204) {
        this.devicesStore.update(id, device);
      }
    });
  }
 deleteDeviceGroup(groupId: string) {
    this.deviceGroupDataService.deleteGroup(groupId).subscribe(deleted => {
      // TODO: remove entity from store
    });
  }


@Component({
  selector: 'marketplace-device-groups',
  templateUrl: './device-groups.component.html',
  styleUrls: ['./device-groups.component.scss']
})
export class DeviceGroupsComponent extends DeviceGroupsBaseComponent
  implements OnInit {
  constructor(public deviceGroupService: DeviceGroupService) {
    super(deviceGroupService);
  }
  deleteGroup(group): void {
    const groupId = group.id;
    this.deviceGroupService.deleteDeviceGroup(groupId);
  }
  ngOnInit() {
    super.ngOnInit();
  }
}

export class DeviceGroupCardComponent implements OnInit {
  isDropdownOpen: Boolean = false;
  @Input() deviceGroup: IDeviceGroup;
  @Output() deleteGroup: EventEmitter<any> = new EventEmitter();
  constructor() {}
  toggleDropdown() {
    if (this.isDropdownOpen) {
      this.isDropdownOpen = false;
    } else {
      this.isDropdownOpen = true;
    }
  }
  deleteGroupClick() {
    this.isDropdownOpen = false;
    console.log(this.deviceGroup);

    this.deleteGroup.emit(this.deviceGroup);
  }
  ngOnInit() {}
}

<div class="dropdown-menu" id="dropdown-menu" role="menu">
        <div class="dropdown-content">
          <a
            routerLinkActive="router-link-active"
            (click)="deleteGroupClick()"
            class="dropdown-item"
          >
            Delete Group
          </a>
        </div>
      </div>
    </div>
更新(id,设备:部分){
此.DeviceDataService.updateDevice(id,设备).subscribe(res=>{
调试器;
if(res['status']&&res['status']==204){
this.devicestore.update(id,设备);
}
});
}
deleteDeviceGroup(组ID:字符串){
this.deviceGroupDataService.deleteGroup(groupId).subscribe(已删除=>{
//TODO:从存储中删除实体
});
}
@组成部分({
选择器:“市场设备组”,
templateUrl:'./device groups.component.html',
样式URL:['./设备组.component.scss']
})
导出类DeviceGroupsComponent扩展DeviceGroupsBaseComponent
实现OnInit{
构造函数(公共设备组服务:设备组服务){
超级(设备组服务);
}
deleteGroup(组):无效{
const groupId=group.id;
this.deviceGroupService.deleteDeviceGroup(groupId);
}
恩戈尼尼特(){
super.ngOnInit();
}
}
导出类DeviceGroupCardComponent实现OnInit{
isDropdownOpen:Boolean=false;
@Input()设备组:IDeviceGroup;
@Output()deleteGroup:EventEmitter=neweventemitter();
构造函数(){}
切换下拉菜单(){
如果(此.isDropdownOpen){
this.isDropdownOpen=false;
}否则{
this.isDropdownOpen=true;
}
}
deleteGroupClick(){
this.isDropdownOpen=false;
console.log(this.deviceGroup);
this.deleteGroup.emit(this.deviceGroup);
}
ngOnInit(){}
}
删除组

id参数属于
update
功能范围。它并不意味着可以在其他地方使用,它实际上是被嵌入的。Subscribe只处理回调,因此它从外部范围继承变量可见性。在您的情况下,第一个订阅继承
id
device
,而
deleteGroup
的订阅继承
groupId
,因此
id
在任何方面都不属于
deleteDeviceGroup
,除非其他地方有全局
id
,我的意思是,在deleteDeviceGroup subscribe中,groupId不可用。我编辑了问题您是否100%确定在调用
deleteDeviceGroup
时首先定义了
groupId
?调用函数时,您是否检查了参数中groupId的值?是,100%定义了它。我也得到了成功的响应,我只需要订阅中的groupId就可以从商店中删除。我可以将id放在服务的这个页面上,但我想了解为什么它不能像更新方法中那样对我可用