admin管理员组文章数量:1418964
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.
本文标签: angularviewChild query returns SignalltElementRefgt instead of SignalltElementgtStack Overflow
版权声明:本文标题:angular - viewChild query returns Signal<ElementRef> instead of Signal<Element> - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745299308a2652274.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论