So it didn't work in the end.

I'll provide some code as to have a better understanding of what's going on.

For clarification's sake I'm also using bindings from AmazeUI JS for 
toggling events when I collapse and expand a div row.


*PARENT COMPONENT*

import { Component, Input, Output, OnInit, AfterViewInit, OnDestroy, 
ViewChild, EventEmitter } from '@angular/core';
import { ParentService } from '../services/parent.service';
import { Observable } from 'rxjs/Rx';
import { ChildComponent } from '../child.component';

@Component({
  selector: 'app-parent',
  templateUrl: './parent.component.html',
  styleUrls: ['./parent.component.scss']
})

export class ParentComponent implements OnInit, AfterViewInit, OnDestroy {
  intervalId;
  parentService;
  dataSubscription;
  syncData = {
    val1: 0,
    val2: 100
  }

  @Input() model = {};
  @Input() index;

  // parentCollapse is the name of the template variable #parentCollapse on 
parent element
  @ViewChild('parentCollapse') parentCollapseChild;
  @ViewChild(ChildComponent) childComponent;

  ngAfterViewInit ()   {
    $(this.parentCollapseChild.nativeElement).on( 'open.collapse.amui', 
this.openCollapsibleParentHandler.bind(this));
    $(this.parentCollapseChild.nativeElement).on( 'close.collapse.amui', 
this.closeCollapsibleParentHandler.bind(this));
  }

  constructor(parentService: ParentService) {
    this.parentService = parentService;
  }

  openCollapsibleParentHandler (event) {
      if ( Object.is(this.parentCollapseChild.nativeElement, event.target) 
) {
        this.dataSubscription = this.showRealTimeData();
      }
      return true;
    };

  closeCollapsibleParentHandler (event) {
      if ( Object.is(this.parentCollapseChild.nativeElement, event.target)) 
{
        this.dataSubscription.unsubscribe();
      }
      return true;
    };

  ngOnInit() {}

  showRealTimeData(pollingInterval: number = 1500) {
    return this.parentService.connectData(pollingInterval).subscribe(
      data => {
        this.syncData.val1 = data.val1;
        this.syncData.val2 = data.val2;
      }
    );
  };

  ngOnDestroy() {
    if(this.dataSubscription) {
      this.dataSubscription.unsubscribe();
    }
  }

}


<div id="parentCollapse-{{index}}" #parentCollapse user-parent-collapse 
class="am-panel-collapse am-collapse detailBelow" 
attr.auto-open-detail-collapse="{{index}}">
  <app-child [model]="model" [index]="index" 
[syncData]="syncData"></app-child>
</div>


*CHILD COMPONENT*

import { Component, OnInit, Input, AfterViewInit, ViewChild } from 
'@angular/core';
import 'rxjs/add/observable/interval';

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

  constructor() { }

  @Input() model;
  @Input() index;
  @Input() syncData: any;

  ngOnInit() {}

}

<div class="child">

  <div class="reach" >{{syncData.val1}}% success</div>
  <div class="rms">{{syncData.val2}} seconds</div>
   
</div>



I've removed unrelated code, but this is essentially how it's structured. 
And the connectData() method returns stubbed data which returns random 
numbers. I know the data works fine because it's only change detection that 
isn't triggered on the child, the data itself is fine and changes in the 
variables. And whenever I make a change in the page afterwards (like 
changing pages etc.) the value then changes.


Thanks for the help in advance!





On Monday, December 19, 2016 at 2:31:22 PM UTC-5, Lucas Lacroix wrote:
>
> It would definitely help to see a code example.
>
> My best guess, without seeing any code, is that the parent component's 
> template is bound to the observable itself while the child is bound so some 
> value returned by the observable and, therefore, does not know when the 
> parent's value has changed. Similar issues can occur if the parent is bound 
> to 'x', such that the value of 'x' is:
> {
>   title: 'Titular Title',
>   data: ...
> }
> and the child is bound to x.title. If you change x in the parent, the 
> child does not know to re-render because it only has a reference to x.title 
> and not x. If you were to change x.title, the child would re-render.
>
> -Luke
>
> On Mon, Dec 19, 2016 at 2:22 PM Andrew Ip <[email protected] 
> <javascript:>> wrote:
>
>> Hi all,
>>
>> I've got a parent component that subscribes to an Observable that returns 
>> some data.
>> That Observable also sets some data in the parent that is passed to a 
>> child component.
>>
>> I'm having an issue though, when the parent's Observable modifies X, and 
>> X is passed to the child, the child template does not show the modified 
>> value of X.
>>
>> I can provide code if need be, but this is a general question about 
>> parent->child value propagation via an Observable.
>>
>> Thanks in advance
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Angular" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected] 
>> <javascript:>.
>> Visit this group at https://groups.google.com/group/angular.
>> For more options, visit https://groups.google.com/d/optout.
>>
> -- 
> Lucas Lacroix
> Computer Scientist
> Advanced Technology Division, MEDITECH <http://ehr.meditech.com/>
> 781-774-2293
>

-- 
You received this message because you are subscribed to the Google Groups 
"Angular" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to