Perhaps I have the wrong setup from the beginning, but I'm having an issue with a child component @Input decorator not updating after the parent's variable has changed. My problem is that the parent's variable is actually getting updated in another child's component that is inherited from the parent.
Sub View Component
@Component({
selector: 'sub-view',
templateUrl: './sub-view.component.html',
styleUrls: ['./sub-view.component.scss']
})
export class SubViewComponent {
@Input() links;
...
}
Parent Component
@Component({
selector: 'parent',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.scss']
})
export class ParentComponent {
roleLinks:any;
constructor() {}
...
}
Parent View
<sub-view
[links]="roleLinks"
></sub-view>
Child Component #1
@Component({
selector: 'child1',
templateUrl: './child1.component.html',
styleUrls: ['./child1.component.scss']
})
export class Child1Component extends ParentComponent {
constructor() {
super();
this.roleLinks = [
{key:'a',val:'A'},
{key:'b',val:'B'}
];
}
...
}
I have Child1Component extends ParentComponent because the idea is that I'll have a Child2, Child3 etc. Each child will have a lot of shared components with Parent
So the SubView will only need to be bound to Parent and then each Child Component will be able to have it's own links
However, I cannot get the links to update in SubView
Looking at other SO Q/A's, I have tried the following:
- adding
ChangeDetectionStrategy.OnPushtoSubView - adding
detectChanges()fromChangeDetectorReftoSubView - adding
markForCheck()fromChangeDetectorReftoSubView
And neither of them worked. links is always undefined inside SubView even though I can see that when it is changed inside Child the changes are actually making it down to Parent however SubView is just not detecting the changes
It may be worth noting that I have each Child component in a nested <router-outlet> within Parent Component.
-App
<router-outlet>
+Page1
+Page2
-Parent
+SubView
<router-outlet>
+Child1
+Child2
+...
+OtherSubView
thisis the current object. So, when you logthis.roleLinksfrom inside the base's ngOnInit method, and this method is in fact called on an instance of Child1,thisis the Child1 instance, andthis.roleLinksis the role links of the Child1 instance. This is fundamental OO stuff. If you have an instance variable (roleLinks) defined in a class, and you create 3 instances of that class, each instance has its own value of this instance variable.