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
Add a comment  | 

1 Answer 1

Reset to default 4

The 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