【问题标题】:Angular 2 Component: How to select an HTML commentAngular 2 组件:如何选择 HTML 注释
【发布时间】:2016-05-19 06:56:20
【问题描述】:

在 AngularJS (1.x) 中,我们可以在 HTML 注释中定义指令:

<!-- directive: my-directive-name -->

How to set up attributes in angularjs directive restricted to comments

如何在 Angular 2 组件选择器中执行此操作(没有 restrict: 'M' 选项)?

更新:看到这个SO问题How to remove/replace the angular2 component's selector tag from HTML,好像replace也不见了……

【问题讨论】:

    标签: angular angular2-directives


    【解决方案1】:

    您不能在评论中这样做,但您应该尝试查看模板,特别是 &lt;template&gt;

    Template Syntax

    你可以这样使用它:

          `<template ngFor #item [ngForOf]="items" #i="index">
            <option *ngIf="isSelected(item)" [value]="item.value" selected>{{item.label}}</option>
            <option *ngIf="!isSelected(item)" [value]="item.value">{{item.label}}</option>
          `</template>
    

    您可以在组件的模板/HTML 中使用它。您仍然需要使用&lt;my-component&gt;&lt;/my-component&gt; 等HTML 标签创建组件,但&lt;template&gt; 标签不会呈现。可能会有所帮助。

    【讨论】:

      【解决方案2】:

      这在 Angular 2 中是不可能的。Angular 2 仅适用于现代浏览器(支持 IE9+),因此组件应使用适用于所有现代浏览器的自定义 Html 元素来定义。

      【讨论】:

      • 这不可能!?问题是我有一些带有 HTML 模板的组件,但它们是虚拟的,不应该出现在 DOM 中(即不应该创建新标签,因为 CSS 类会有问题)
      • 使用标签选择器
      猜你喜欢
      • 1970-01-01
      • 2017-01-04
      • 1970-01-01
      • 2017-03-24
      • 2017-01-24
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多