I'm trying to make a custom counter input component, and I'm struggling to have the input value controlled by the custom increment/decrement buttons.
This is what I'm trying to make:
I want to use content projection and have the input exposed in order to use it in forms and add properties as I would on a regular number input. So a directive on an input seems like the way to go:
<my-counter-input>
<input [(ngModel)]="count" myInputRef />
</my-counter-input>
The component itself would have two buttons and a ng-content-slot:
<button (click)="decrement()">-</button>
<ng-content select="input"></ng-content>
<button (click)="increment()">+</button>
So far everything seems alright. Now I can interact with the directive by using the @ContentChild decorator.
@Component({
selector: 'my-counter-input',
templateUrl: './counter-input.component.html',
styleUrls: ['./counter-input.component.scss'],
})
export class CounterInputComponent {
@ContentChild(InputRefDirective)
public input: InputRefDirective;
constructor() {}
public increment() {
this.input.increment();
}
public decrement() {
this.input.decrement();
}
}
I guess this is where the problem arises. I'm binding the value of the input element to the directive, but neither increment/decrement method has any effect on the native input value. I'd like to have a two-way binding on the native input value, but it seems like this is not the case.
@Directive({
selector: 'input [myInputRef]',
})
export class InputRefDirective {
@HostBinding('type') readonly type = 'number';
@HostBinding('value') value = 5;
public increment() {
this.value++;
}
public decrement() {
this.value--;
}
}
I'm not sure how I should go about this. How can I update the value of the native input and also trigger the native change event?
