Trung BìnhAngular iconAngular

View query và content query khác nhau thế nào?

View query đọc element/component nằm trong template của chính component; content query đọc nội dung parent project vào qua ng-content.

Ví dụ signal query hiện đại:

typescript
@Component({ template: `<input #searchBox />` })
export class SearchPanel {
  searchBox = viewChild<ElementRef<HTMLInputElement>>("searchBox")

  focus() {
    this.searchBox()?.nativeElement.focus()
  }
}

Dùng query khi cần tương tác với child component/directive hoặc DOM API thật.

Không dùng query chỉ để truyền data; việc đó nên qua input/output hoặc service/store.

Xem toàn bộ Angular cùng filter theo level & chủ đề con.

Mở danh sách Angular