2017-08-18 144 views
-2

如何正确访问formBuilder中的this.existingUsers?现在console.log(value)不显示在控制台中。 console.log(this.id)返回正确的参数。Angular 2 - 访问订阅值

export class UserComponent implements OnInit { 

    existingUser: any = {}; 

     ngOnInit() { 

     this.activatedRoute.params.subscribe((params: Params) => { 
      this.id = params['id']; 
      console.log(this.id); 
      this.db.object(`/users/${this.id}`).map(value => { 
      console.log(value); 
      this.existingUser = value; 
      }) 
     }); 

     console.log(this.existingUser); 

     this.userForm = this.formBuilder.group({ 
      first_name: [this.existingUser.first_name, Validators.required], 
     }) 
     }; 

} 
+0

你不显示什么'this.db.object'可能是,但你永远不'subscribe'什么返回。另外你似乎认为'existingUser'会以某种方式在后续行中可用 - 如果是这种情况,*你不需要可观察的*。 – jonrsharpe

+0

[如何从异步回调函数返回值?](https://stackoverflow.com/questions/6847697/how-to-return-value-from-an-asynchronous-callback-function) –

回答

0

我并不完全清楚你正在尝试做什么......但你可能需要.map方法中的代码。就像这样:

export class UserComponent implements OnInit { 

    existingUser: any = {}; 

    ngOnInit() { 

    this.activatedRoute.params.subscribe((params: Params) => { 
     this.id = params['id']; 
     console.log(this.id); 
     this.db.object(`/users/${this.id}`).map(value => { 
     console.log(value); 
     this.existingUser = value; 
     console.log(this.existingUser); 

     this.userForm = this.formBuilder.group({ 
      first_name: [this.existingUser.first_name, Validators.required], 
     }) 
     }) 
    }); 

    }; 

}

或者把这些代码在一个被从这里调用的方法。

更新:这是我的应用程序,做类似的例子:

ngOnInit(): void { 
    this.productForm = this.fb.group({ 
     productName: ['', [Validators.required, 
          Validators.minLength(3), 
          Validators.maxLength(50)]], 
     productCode: ['', Validators.required], 
     starRating: ['', NumberValidators.range(1, 5)], 
     tags: this.fb.array([]), 
     description: '' 
    }); 

    // Read the product Id from the route parameter 
    this.route.params.subscribe(
     params => { 
      let id = +params['id']; 
      this.getProduct(id); 
     } 
    ); 
} 

getProduct(id: number): void { 
    this.productService.getProduct(id) 
     .subscribe(
      (product: IProduct) => this.onProductRetrieved(product), 
      (error: any) => this.errorMessage = <any>error 
     ); 
} 

onProductRetrieved(product: IProduct): void { 
    if (this.productForm) { 
     this.productForm.reset(); 
    } 
    this.product = product; 

    if (this.product.id === 0) { 
     this.pageTitle = 'Add Product'; 
    } else { 
     this.pageTitle = `Edit Product: ${this.product.productName}`; 
    } 

    // Update the data on the form 
    this.productForm.patchValue({ 
     productName: this.product.productName, 
     productCode: this.product.productCode, 
     starRating: this.product.starRating, 
     description: this.product.description 
    }); 
    this.productForm.setControl('tags', this.fb.array(this.product.tags || [])); 
} 

您可以找到全套的代码在这里:https://github.com/DeborahK/Angular2-ReactiveForms(在APM文件夹)

+0

I' m试图使用相同的组件来创建/更新用户。这就是为什么我订阅this.activatedRoute.params。我忽略了如果在我的问题。如果this.id不是create,并且如果this.db.object(/ users/$ {this.id})返回一个用户,则表单将填充用户的信息 – Ciprian

+0

然后所有这些逻辑都可以进入方法例如在'this.existingUser = value'语句之后调用。 – DeborahK

+0

如果您想查看完整的创建,更新,删除,读取(CRUD)的代码示例,我有一个完整的示例应用程序在这里:https://github.com/DeborahK/Angular2-ReactiveForms(在APM文件夹中) – DeborahK

-2

您正在尝试在设置数据之前获取asynchronous数据。

当某些内容异步时,它将在将来运行,并且订阅是异步的。

订阅功能中的所有内容在ngOnInit已完成后运行。这意味着,在existing_user被设置之前,您正在尝试访问first_name

请尝试移动this.userForm东西到订阅功能如下:

this.activatedRoute.params.subscribe((params: Params) => { 
     this.id = params['id']; 
     console.log(this.id); 
     this.db.object(`/users/${this.id}`).map(value => { 
     console.log(value); 
     this.existingUser = value; 
     }) 
     console.log(this.existingUser); 

     this.userForm = this.formBuilder.group({ 
     first_name: [this.existingUser.first_name, Validators.required], 
     }) 
    });