0
我将一个组件的值传递给另一个具有动态形式的组件,但在该组件中没有接收到值。如果我在向动态表单传递值时做错了任何事情,或者这不是动态表单可以获得价值的方式,如何订阅动态形式的角度值2
我没有粘贴整个代码,否则它会太大,所以我只提到相关的代码段。请让我知道是否需要更多的澄清。
在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();}
是您的解决方案worked.Thanks alot.But,有一次我有一个问题如果通过getter/setter可以实现同样的事情,为什么要引入Subject。我是新来的角,所以我想了解两种方法的优缺点。 – user2800089
'主题'不是一个有趣的事情,它是一个RxJS事物(Reactive Extensions)。你只需要它们来构建你自己的pub/sub模型的更复杂的案例。使用Angular已有的功能时,您不需要它,例如更改检测。这里是一篇博客文章,其中涵盖了一些细节:https://stackoverflow.com/questions/34849873/what-are-the-semantics-of-different-rxjs-subjects/34860777#34860777 – DeborahK