@@ -180,10 +180,6 @@ export class MatDatepickerContent<S, D = ExtractDateTypeFromSelection<S>>
180180 }
181181
182182 ngOnInit ( ) {
183- // If we have actions, clone the model so that we have the ability to cancel the selection,
184- // otherwise update the global model directly. Note that we want to assign this as soon as
185- // possible, but `_actionsPortal` isn't available in the constructor so we do it in `ngOnInit`.
186- this . _model = this . _actionsPortal ? this . _globalModel . clone ( ) : this . _globalModel ;
187183 this . _animationState = this . datepicker . touchUi ? 'enter-dialog' : 'enter-dropdown' ;
188184 }
189185
@@ -246,6 +242,25 @@ export class MatDatepickerContent<S, D = ExtractDateTypeFromSelection<S>>
246242 this . _globalModel . updateSelection ( this . _model . selection , this ) ;
247243 }
248244 }
245+
246+ /**
247+ * Assigns a new portal containing the datepicker actions.
248+ * @param portal Portal with the actions to be assigned.
249+ * @param forceRerender Whether a re-render of the portal should be triggered. This isn't
250+ * necessary if the portal is assigned during initialization, but it may be required if it's
251+ * added at a later point.
252+ */
253+ _assignActions ( portal : TemplatePortal < any > | null , forceRerender : boolean ) {
254+ // If we have actions, clone the model so that we have the ability to cancel the selection,
255+ // otherwise update the global model directly. Note that we want to assign this as soon as
256+ // possible, but `_actionsPortal` isn't available in the constructor so we do it in `ngOnInit`.
257+ this . _model = portal ? this . _globalModel . clone ( ) : this . _globalModel ;
258+ this . _actionsPortal = portal ;
259+
260+ if ( forceRerender ) {
261+ this . _changeDetectorRef . detectChanges ( ) ;
262+ }
263+ }
249264}
250265
251266/** Form control that can be associated with a datepicker. */
@@ -556,6 +571,7 @@ export abstract class MatDatepickerBase<
556571 throw Error ( 'A MatDatepicker can only be associated with a single actions row.' ) ;
557572 }
558573 this . _actionsPortal = portal ;
574+ this . _componentRef ?. instance . _assignActions ( portal , true ) ;
559575 }
560576
561577 /**
@@ -565,6 +581,7 @@ export abstract class MatDatepickerBase<
565581 removeActions ( portal : TemplatePortal ) : void {
566582 if ( portal === this . _actionsPortal ) {
567583 this . _actionsPortal = null ;
584+ this . _componentRef ?. instance . _assignActions ( null , true ) ;
568585 }
569586 }
570587
@@ -632,8 +649,8 @@ export abstract class MatDatepickerBase<
632649 protected _forwardContentValues ( instance : MatDatepickerContent < S , D > ) {
633650 instance . datepicker = this ;
634651 instance . color = this . color ;
635- instance . _actionsPortal = this . _actionsPortal ;
636652 instance . _dialogLabelId = this . datepickerInput . getOverlayLabelId ( ) ;
653+ instance . _assignActions ( this . _actionsPortal , false ) ;
637654 }
638655
639656 /** Opens the overlay with the calendar. */
0 commit comments