Created
July 26, 2017 18:45
-
-
Save chrillewoodz/a62492521eb6692ea9a6aa07052b77e2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
ChangeDetectionStrategy, | |
Component, | |
forwardRef, | |
Input | |
} from '@angular/core'; | |
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms'; | |
// Custom | |
import {Utils} from '@services/utils.service'; | |
import {CustomValidators} from '@validators/custom-validators'; | |
@Component({ | |
moduleId: module.id, | |
selector: 'switch', | |
templateUrl: './switch.component.html', | |
styleUrls: ['./switch.component.scss'], | |
providers: [ | |
{provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => SwitchComponent), multi: true} | |
], | |
changeDetection: ChangeDetectionStrategy.OnPush | |
}) | |
export class SwitchComponent implements ControlValueAccessor { | |
@Input() disabled: boolean; | |
private _model: boolean; | |
public id: string = Utils.getUniqueID(); | |
constructor() {} | |
get model() { | |
return this._model; | |
} | |
set model(val) { | |
this._model = val; | |
this.propagateChange(val); | |
} | |
propagateChange: any = () => {}; | |
validateFn: any = () => {}; | |
writeValue(value) { | |
if (value) { | |
this.model = value; | |
} | |
} | |
registerOnChange(fn) { | |
this.propagateChange = fn; | |
} | |
registerOnTouched() {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment