什么是 Angular 中组件的 Host 元素

Man*_*dha 4 angular6

如果我有 Component 类

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
}
Run Code Online (Sandbox Code Playgroud)

和 index.html 是

<div>
<my-app></my-app>
</div>
Run Code Online (Sandbox Code Playgroud)

组件的宿主元素是什么?会是<div>还是<my-app>?如果是<my-app>,有没有办法从 访问<div>(的父AppComponentAppComponent

Man*_*dha 12

对于指令,它是指令附加到的元素。例如h1是下面示例中的宿主元素

<h1 my-directive>
</my-comp>
Run Code Online (Sandbox Code Playgroud)

对于组件,它是组件的选择器。例如

selector: 'my-comp'
template: '
<h1> some heading </h1>
.....

<my-comp></my-comp>  <!-- my-comp is the host element of h1 -->
Run Code Online (Sandbox Code Playgroud)