I have a component on my root page that exposes a few bindable properties and can be used like this:
<myprogress [value]="current" [max]="maximum"></myprogress>
In my application, various types objects require progress display. So I created some directives like below:
@Directive({
selector: 'myprogress[order]'
})
export class OrderProgress {
@Input('order') order: Order;
constructor(private baseComponent: Progress) {}
ngOnInit() {
this.baseComponent.value = this.order.currentStage;
this.baseComponent.max = this.order.totalStages;
}
}
This allows me to replace calls like:
<myprogress [value]="order.currentStage" [max]="order.totalStages"></myprogress>
with a more readable:
<myprogress [order]="order"></myprogress>
This works fine until some part of the application changes the order itself. In that case, myprogress
components which have already been rendered, are not updated. And I can see why too. The ngOnInit
function runs just once when the component is rendered and there is no way to indicate to the directive that some property of the order has changed and new values for value
and max
need to be calculated.
Is there a way to solve this problem?
One possible solution I have is this:
- Modify
Progress
component to accept numbers as well as a function that returns a number. - In
OrderProgress
'sngOnInit
, bind as follows:this.baseComponent.value = () => this.order.currentStage
But it requires me to change Progress
component as well. Is there a way which doesn't require me to change Progress
or Order
?
Subject
, if I understand you clearly. You want the changes in parent components order reflected in the child directive. Wouldnt change detection change value of order in child directive when value of order is mutated in parent ? – raj