简体   繁体   English

Angular Observable&Subject无法获取数据

[英]Angular Observable & Subject not getting the data

I'm very new in angular. 我在角度方面很新。

I have created one service given below. 我创建了以下一项服务。

import { Injectable } from '@angular/core';
import { Observable, Subject } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class WizardDataService {

  constructor() { }

  private txtBxServiceID=new Subject<any>();

  setTxtBxServiceID(value:any){
    this.txtBxServiceID.next({txtBxServiceID:value});
  }

  getTxtBxServiceID(): Observable<any> {
    return this.txtBxServiceID.asObservable();
  }


}

I'm setting the txtBxServiceID value in another component on form submission. 我在表单提交的另一个组件中设置txtBxServiceID值。 given below. 如下。

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from  '@angular/forms';
import { Router } from  '@angular/router';
import {WizardDataService} from '../services/wizard-data.service'

@Component({
  selector: 'app-discover-device',
  templateUrl: './discover-device.component.html',
  styleUrls: ['./discover-device.component.scss']
})
export class DiscoverDeviceComponent implements OnInit {

  discoverDeviceComponentForm: FormGroup;
  isSubmitted  =  false;

  constructor(private router: Router, private formBuilder: FormBuilder,private wizardDataService : WizardDataService) { }

  ngOnInit() {
    this.discoverDeviceComponentForm  =  this.formBuilder.group({
      txtBxServiceID: ['',  Validators.required]
  });
  }
  get formControls() { return this.discoverDeviceComponentForm.controls; }

  dicoverDevice(){
    this.isSubmitted=true;
    if(this.discoverDeviceComponentForm.invalid){
      return;
    }
    this.wizardDataService.setTxtBxServiceID(this.discoverDeviceComponentForm.value.txtBxServiceID);

    this.router.navigateByUrl('mits-update/device-details');
  }
}

now I am trying to access the service on another component. 现在,我正在尝试访问另一个组件上的服务。 Like below. 像下面。

import { Component, OnInit} from '@angular/core';
import {WizardDataService} from '../services/wizard-data.service';


@Component({
  selector: 'app-device-mits-update',
  templateUrl: './device-mits-update.component.html',
  styleUrls: ['./device-mits-update.component.scss']
})
export class DeviceMitsUpdateComponent implements OnInit {

  txtBxServiceID:any="";

  constructor(private wizardDataService:WizardDataService) {

   this.wizardDataService.getTxtBxServiceID().subscribe(value =>{
      this.txtBxServiceID=value.txtBxServiceID;
    });
  }

  ngOnInit() {
  }

}

then I am displaying this value on my HTML like 然后我在HTML上显示此值,例如

 <td>Service Id</td>
        <td>{{txtBxServiceID}} </td>

My issue is the value is not displaying here. 我的问题是该值未在此处显示。 So can anyone help me here to find what is the mistake I have done here? 那么有人可以在这里帮助我找到我在这里犯的错误吗?

As suggested by Daniel, you can use BehaviorSubject. 根据Daniel的建议,您可以使用BehaviorSubject。 However, it requires an initial value. 但是,它需要一个初始值。 Since you are not providing any value you can use ReplaySubject . 由于您没有提供任何值,因此可以使用ReplaySubject

private txtBxServiceID = new ReplaySubject<any>(1);

You have a simple Subject . 您有一个简单的Subject You subscribe to it in the new component only after the first component has set the value. 仅在第一个组件设置了值之后,您才能在新组件中订阅它。 Subject does not replay previous values to new subscribers. Subject不会将以前的值重播给新订户。 It also doesn't hold the latest value and sends that to new subscribers. 它还不保存最新值,并将其发送给新订户。

Most likely, you want a BehaviorSubject , it will store the latest value and send it to new subscribers. 最有可能的是,您需要一个BehaviorSubject ,它将存储最新值并将其发送给新订户。

If you want all previous values sent to new subscribers, you need to use a ReplaceSubject instead. 如果要将所有以前的值发送给新订阅者,则需要使用ReplaceSubject

So, use the following code instead of what you have now: 因此,请使用以下代码代替现在的代码:

private txtBxServiceID = new BehaviorSubject<any>(null);

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

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