我刚开始把我的Angular 1.x指令转换为Angular 2组件,但我卡在了第一步。我有两个指令,分别是row-field和column-field。它们可以用来创建bootstrap的12个单元格网格,如下所示:
<row-field>
<column-field span="4">First Cell</column-field>
<column-field span="2" offset="1">Second Cell</column-field>
<column-field span="4">Third Cell</column-field>
</row-field>
这将会输出如下格式的HTML代码:
<div class="row">
<div class="span4">First Cell</div>
<div class="span2 offset1">Second Cell</div>
<div class="span4">Third Cell</div>
</div>
在 Angular 1.x 中,使用指令的 tranclude 和 replace 属性非常容易实现。但我在 Angular 2 中创建同样的行为时遇到了困难。我的 column-field 组件如下:
@Component({
selector: 'column-field',
template: '<ng-content [ngClass]="'span{{span}} offset{{offset}}'"></ng-content>',
directives: [NgClass]
})
export class ColumnFieldComponent {
@Input() span;
@Input() offset;
}
但是它并没有产生期望的HTML输出。输出中总会有一个column-field标签,我想要用div标签替换column-field标签,并将其属性移动到这个新的div标签。任何建议将不胜感激。
column-field
标签。请参见Günter在此处的答案:https://dev59.com/q1kT5IYBdhLWcg3wXuTP#38716164 - Greg