Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.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
Javascript 存储service.ts中的数据以在同一service.ts中使用_Javascript_Angularjs - Fatal编程技术网

Javascript 存储service.ts中的数据以在同一service.ts中使用

Javascript 存储service.ts中的数据以在同一service.ts中使用,javascript,angularjs,Javascript,Angularjs,我有firestore.service.ts,在一个方法中,我在firebase中创建并更新了一个集合。。。离开该方法后,我更改了集合的“id”,但我需要确保这个“id”不会丢失,因为我需要将它用于下一个方法。 如何保留此“id”值 公共createFormulario(数据){ 返回此.firestore.collection('formularios')。添加(数据)。然后 (参考=>{ data.id=ref.id;您可以在服务/类中声明一个变量,例如id:number;这可以在类中的任何

我有firestore.service.ts,在一个方法中,我在firebase中创建并更新了一个集合。。。离开该方法后,我更改了集合的“id”,但我需要确保这个“id”不会丢失,因为我需要将它用于下一个方法。 如何保留此“id”值

公共createFormulario(数据){ 返回此.firestore.collection('formularios')。添加(数据)。然后 (参考=>{
data.id=ref.id;您可以在服务/类中声明一个变量,例如
id:number;
这可以在类中的任何位置引用

因此,在您的
createFormulario
方法中,您可以说
this.id=ref.id;
这将全局存储id(在服务中),您可以在
createPreguntal
方法中将其引用为
this.id

export class FirestoreService {
  //declare our class variables
  id: number;

  public createFormulario ( data ) {
      return this.firestore.collection('formularios').add(data).then
          (ref => {
            this.id = ref.id; //store the id in our class variable
            this.firestore.collection('formularios').doc( ref.id ).update(data);
          });
  }

  public createPregunta1 ( data: any ) {
      this.firestore.collection('formularios').doc(this.id).update(data);
  }
}

你可以在你的服务/类中声明一个变量,例如id:number;然后在你的createFormulario方法中你可以这样说。id=ref.id;这将在服务中全局存储它,你可以在createPreguntal方法中引用它,如下所示。idies!!!这是正确的答案。但是我不使用number…而是使用string,非常感谢。th就在那时spot@SergioJoséVillalobosAguilar太好了,没问题:)@SergioJoséVillalobosAguilar很高兴能提供帮助。如果您认为这是您所需要的,请随意标记答案。(问题左侧有一个勾号。)谢谢:)
export class FirestoreService {
  //declare our class variables
  id: number;

  public createFormulario ( data ) {
      return this.firestore.collection('formularios').add(data).then
          (ref => {
            this.id = ref.id; //store the id in our class variable
            this.firestore.collection('formularios').doc( ref.id ).update(data);
          });
  }

  public createPregunta1 ( data: any ) {
      this.firestore.collection('formularios').doc(this.id).update(data);
  }
}