javascript - DOM 中没有选择器标记的 Angular 2 组件

标签 javascript angular angular2-template angular2-directives

我想要这个:

<div *ngIf="...">div 1...</div>
<div *ngIf="...">div 2...</div>
<div *ngIf="...">div 3...</div>

但我不想重复 *ngIf ,所以我创建了我的组件 <my-component> , 使用此模板:

<div>div 1...</div>
<div>div 2...</div>
<div>div 3...</div>

然后我将 *ngIf 放在我的组件标签中:<my-component *ngIf="...">

问题是 Angular 2 将 <my-component> DOM 中的标记,我不想要它。

对于了解 ASP.NET WebForms 的任何人,我想要 Angular 2 中的一个组件,其工作方式类似于 <asp:PlaceHolder>控制...

最佳答案

To answer your question, you can also do this...

@Component({
  selector: '[my-component]'...

<my-component *ngIf="..."</my-component>

// becomes this in the dom

<div my-component _nghost...>

There is also ng-container for this purpose.

<ng-container *ngIf="something.is.there">
  <div class="here">
    Hello
  </div>
</ng-container>

// DOM: => <div class="here">Hello</div>

关于javascript - DOM 中没有选择器标记的 Angular 2 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38287224/

相关文章:

javascript - 按年份和月份对数组进行排序

javascript - Angular JS 2.0 基于用户输入切换样式表

Angular2 路由器链接不工作

javascript - 显示事件不适用于 Bootstrap Collapse

javascript - 在 WordPress 自定义模板中使用 Jquery .click() 实现选项卡

javascript - 获得意外的 token 导出

angular - 扩展HttpErrorResponse导致错误找不到模块 '@angular/common/http'

javascript - HTML5/Javascript - 为什么我的动画在第一次开始时会跳跃

Angular js 4 与 VS Code 在 powershell 中的 npm start 命令上显示错误

Angular *ngIf 内存使用