Angular. ViewChild, ViewChildren, ContentChild и ContentChildren

Angular предоставляет 4 декоратора свойств @ViewChildren, @ViewChild, @ContentChildren и @ContentChild для доступа к дочерним элементам:

  • @ViewChild - querySelector
  • @ViewChildren - querySelectorAll
  • @ContentChild - querySelector внутри <ng-content>
  • @ContentChildren - querySelectorAll внутри <ng-content>

viewchildren-viewchild-contentchildren-and-contentchild

  1. <todo-input> и <todo-item> рассматривается как ViewChildren <todo-app>
  2. <app-footer> (если он определен как Angular компонент или директива) рассматривается как ContentChild.

Похожие записи

Как добавить ng-bootstrap компоненты в проект Angular-CLI?

Покажу на примере нового проекта.

ng new project_name
cd project_name
npm install --save bootstrap@next
npm install --save @ng-bootstrap/ng-bootstrap

В angular-cli.json в секцию style надо добавить наш CSS, чтобы глобально подключить стили.

  "styles": [
    "styles.css",
    "../node_modules/bootstrap/dist/css/bootstrap.min.css"
  ],
17 августа 2017 г. в Angular

Angular routerLink conditionally

<a [routerLink]="myVar ? '/home' : null" routerLinkActive="is-active">Home</a>
or
<a [routerLink]="myVar ? ['/home'] : []">Home</a>
02 сентября 2019 г. в Angular

Angular environment service

Использование сервиса для окружения вместо прямой ссылки на environment.ts