I'm using viewChild
to get reference to some element, I expect the signal to return a reference to the element but it seems to return the old syntax {nativeElement: Element}
instead
Package: @angular/[email protected]
import {Component, signal, computed, viewChild} from '@angular/core';
import {bootstrapApplication} from '@angular/platform-browser';
@Component({
selector: 'app-root',
template: `
<h2>Cookie recipe</h2>
<label>
# of cookies:
<input #input type="range" min="10" max="100" step="10" [value]="count()" (input)="update($event)" />
{{ count() }}
</label>
<p>Butter: {{ butter() }} cup(s)</p>
<p>Sugar: {{ sugar() }} cup(s)</p>
<p>Flour: {{ flour() }} cup(s)</p>
<button
type="button"
(click)="log()"
>Log</button>
`,
})
export class CookieRecipe {
count = signal(10);
input = viewChild<HTMLInputElement>("input")
butter = computed(() => this.count() * 0.1);
sugar = computed(() => this.count() * 0.05);
flour = computed(() => this.count() * 0.2);
update(event: Event) {
const input = event.target as HTMLInputElement;
this.count.set(parseInt(input.value));
}
log() {
alert(`input: ${this.input()?.nativeElement.value}`) // Works but typescript complains
alert(`input: ${this.input()?.value}`) // Doesn't work but fine with typescript
}
}
bootstrapApplication(CookieRecipe);
I'm using viewChild
to get reference to some element, I expect the signal to return a reference to the element but it seems to return the old syntax {nativeElement: Element}
instead
Package: @angular/[email protected]
import {Component, signal, computed, viewChild} from '@angular/core';
import {bootstrapApplication} from '@angular/platform-browser';
@Component({
selector: 'app-root',
template: `
<h2>Cookie recipe</h2>
<label>
# of cookies:
<input #input type="range" min="10" max="100" step="10" [value]="count()" (input)="update($event)" />
{{ count() }}
</label>
<p>Butter: {{ butter() }} cup(s)</p>
<p>Sugar: {{ sugar() }} cup(s)</p>
<p>Flour: {{ flour() }} cup(s)</p>
<button
type="button"
(click)="log()"
>Log</button>
`,
})
export class CookieRecipe {
count = signal(10);
input = viewChild<HTMLInputElement>("input")
butter = computed(() => this.count() * 0.1);
sugar = computed(() => this.count() * 0.05);
flour = computed(() => this.count() * 0.2);
update(event: Event) {
const input = event.target as HTMLInputElement;
this.count.set(parseInt(input.value));
}
log() {
alert(`input: ${this.input()?.nativeElement.value}`) // Works but typescript complains
alert(`input: ${this.input()?.value}`) // Doesn't work but fine with typescript
}
}
bootstrapApplication(CookieRecipe);
Share
Improve this question
edited Mar 1 at 15:03
Giannis
1,8401 gold badge13 silver badges33 bronze badges
asked Jan 29 at 12:07
ka_fuachieka_fuachie
381 silver badge6 bronze badges
1 Answer
Reset to default 4The typing should be viewChild<ElementRef<HTMLInputElement>>('input');
You can't access the DOM element directly with a query.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745299309a4621338.html
评论列表(0条)