Skip to content

Instantly share code, notes, and snippets.

@NetanelBasal
Last active April 2, 2021 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NetanelBasal/a8d70da6e871756e0e6a9783e6564625 to your computer and use it in GitHub Desktop.
Save NetanelBasal/a8d70da6e871756e0e6a9783e6564625 to your computer and use it in GitHub Desktop.
@Component({
template: `...<button (click)="clear()">X</button>`
})
export class InputComponent {
ref: InputRefDirective;
@ContentChild(InputRefDirective) set inputRef(ref: InputRefDirective) {
this.ref = ref;
}
clear() {
this.ref.control.patchValue('');
}
}
@NgModule({
declarations: [InputComponent, InputRefDirective],
exports: [InputComponent, InputRefDirective]
})
export class InputModule {}
@Directive({
selector: '[inputRef]'
})
export class InputRefDirective {
constructor(@Optional() private ngControl: NgControl) {
}
get control() {
return this.ngControl?.control;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment