Created
December 12, 2019 17:42
-
-
Save Kalpesh300/0853ce7d1f86103ec93457b2bf72dbb1 to your computer and use it in GitHub Desktop.
Custom Dropdown Component
This file contains 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 { Component, OnInit, forwardRef } from '@angular/core'; | |
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; | |
@Component({ | |
selector: 'app-custom-dropdown', | |
templateUrl: './custom-dropdown.component.html', | |
styleUrls: ['./custom-dropdown.component.scss'], | |
providers: [{ | |
provide: NG_VALUE_ACCESSOR, | |
useExisting: forwardRef(() => CustomDropdownComponent), | |
multi: true, | |
}] | |
}) | |
export class CustomDropdownComponent implements OnInit, | |
ControlValueAccessor { | |
options: string[]; | |
selectedOption: string; | |
onChange: (_: any) => {}; | |
constructor() { } | |
ngOnInit() { | |
this.options = [ | |
'option 1', | |
'option 2', | |
'option 3', | |
]; | |
this.selectedOption = this.options[0]; | |
} | |
writeValue(value: string) { | |
this.selectedOption = value; | |
} | |
registerOnChange(fn: (_: any) => {}) { | |
this.onChange = fn; | |
} | |
changeSelectedOption(option: string) { | |
this.selectedOption = option; | |
this.onChange(option); | |
} | |
registerOnTouched() { } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment