0

我将值从一个组件传递到另一个具有动态形式的组件,但在该组件中,没有接收到值。如果我在将值传递给动态表单时做错了什么,或者这不是动态表单接收值的方式,我不会得到。

我没有粘贴整个代码,否则它会太大,所以我只提到了相关的代码。如果需要进一步说明,请告诉我。

在 AdminService 中,这是服务类,其中主题定义如下:

startedEditingItem=new Subject<string>();    

在 ProductItemsComponent 中,值如下发送到另一个组件。

onEdit(index:number){
    console.log("on edit"+this.productId+":"+this.childProductId+":"+index)// This is being printed , so value is sent
    this.adminService.startedEditingItem.next(this.productId+":"+this.childProductId+":"+index);}

在 ItemEditComponent 中,订阅值如下

ngOnInit() {
    console.log("edit item on init") // printed on console
    this.subscription=this.adminService.startedEditingItem.subscribe(
      (id:string)=>{
        console.log("edit item>"+id); // not printed on console.so value is not received
        this.initForm();});
    this.initForm();}
4

1 回答 1

0

你真的需要Subject这里吗?您可以使用简单的服务更轻松地进行通信,而无需使用Subject. 例如:

import { Injectable } from '@angular/core';

@Injectable() 
export class DataService {
  serviceData: string; 
}

您可以在此处找到更多详细信息:https ://blogs.msmvps.com/deborahk/build-a-simple-angular-service-to-share-data/

还有一个笨蛋:

https://plnkr.co/edit/KT4JLmpcwGBM2xdZQeI9?p=preview

于 2017-08-09T17:33:53.330 回答