I would like to inject conditionally a service depending on @Input
value
service.ts
import { Injectable } from '@angular/core';
import { DateAdapter } from '@angular/material/core';
import {
MatDateRangeSelectionStrategy,
DateRange,
} from '@angular/material/datepicker';
@Injectable()
export class FiveDayRangeSelectionStrategy<D>
implements MatDateRangeSelectionStrategy<D>
{
constructor(private _dateAdapter: DateAdapter<D>) {}
selectionFinished(date: D | null): DateRange<D> {
return this._createFiveDayRange(date);
}
createPreview(activeDate: D | null): DateRange<D> {
return this._createFiveDayRange(activeDate);
}
private _createFiveDayRange(date: D | null): DateRange<D> {
if (date) {
const start = this._dateAdapter.addCalendarDays(date, -2);
const end = this._dateAdapter.addCalendarDays(date, 2);
return new DateRange<D>(start, end);
}
return new DateRange<D>(null, null);
}
}
component.ts
import { Component } from '@angular/core';
import { MAT_DATE_RANGE_SELECTION_STRATEGY } from '@angular/material/datepicker';
import { SelectionStrategy } from './selectionStrategy.service';
/** @title Date range picker with custom a selection strategy */
@Component({
selector: 'date-range-picker-selection-strategy-example',
templateUrl: 'date-range-picker-selection-strategy-example.html',
providers: [
{
provide: MAT_DATE_RANGE_SELECTION_STRATEGY,
useClass: SelectionStrategy,
},
],
})
export class DateRangePickerSelectionStrategyExample {}
@Input() isStrategy:boolean=false // inject service if true *
How can I provide service depending on isStrategy
Input ?
Here's an example