I have a parent and child component. Parent component has index and passes this through to the child component as an @Input. This index value constantly changes in the parent component, and inside my child component, I want to run a function everytime the Parent component changes index. This has a slide component that constantly changes it. How can I achieve this? I've tried with the below code (this.index.subscribe(() =>), but I'm getting that it's not a function everytime I try and initialise the subscription.
EDIT: There is no related code in the Child template that could impact this, so it's not provided. ngOnChange doesn't seem to work as the change is happening in the directive as opposed to the parent's template.
Child:
import {Component, OnInit, ViewChild, Input} from '@angular/core';
import {Observable} from 'rxjs/Observable';
@Component({
selector: "child",
templateUrl: "components/child/child.html",
})
export class ChildComponent implements OnInit {
@Input() index: string;
currentIndex: any;
constructor() {}
ngOnInit() {}
ngOnChanges(){}
ngAfterViewInit() {
console.log("index " + this.index);
this.currentIndex = this.index.subscribe(() => {
console.log("index " + this.index);
})
}
ngOnDestroy() {
this.currentIndex.unsubscribe();
}
}
Parent:
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {Page} from "ui/page";
import {ChildComponent} from '/components/child/child.component'
@Component({
selector: "parent",
template: "<child [index]="index"></child>",
directives: [ChildComponent]
})
export class ParentComponent implements OnInit {
index: string = "0,1";
constructor(private page: Page) {
}
}
ParentComponent's template should betemplate, nottemplateUrl. Besides, could you show the template forChildComponent?indexinParentComponentis astringandChildComponentexpects it to be anObservable<any>. Is that how you are using them?Error: this.index.subscribe is not a function.templatethis.slidescoming from? From your code, it can't be anything other thanundefined(it is not being set anywhere), which throws an error atlet SlidesXml = this.slides.nativeElement;.